maven 本地、测试、生产打包

来源:互联网 发布:mac菜单栏如何清除 编辑:程序博客网 时间:2024/05/23 01:16

前一篇文章也是maven的本地、测试、生产打包的一种方式,而且网上大都也是这种方式,而这篇文章是领导要求这么弄的也就这么弄了,下面来看看

一、工程目录


注意一下两个红框哦,上面的那个框是没有改动的也不用改动而下面的这个红框中我们添加了一个_online 和_test 两个文件夹其中的配置的东西和resources是一样的只是路径、数据库地址什么的不一样而已,resources文件夹中的配置就是本地开发环境的配置而resources_online 就是线上环境的配置,resources_test就是测试环境上的配置。

注意:上篇文章中我们修改了上面的而这篇文章中修改了下面的

二、配置文件

1、默认资源加载路径配置:

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--配置默认值--> <package.target>test</package.target></properties>
其中的package.target的值就是配置的默认值,这个值和resources_test、resources_online 这个有关,其实在这个demo中配置的值就是resources_的后缀。这个一定要配置不然要么报错要么打包大不了!!!

2、profiles配置:

<profiles>        <profile>            <id>test</id>            <properties>                <package.target>test</package.target>            </properties>        </profile>        <profile>            <id>online</id>            <properties>                <package.target>online</package.target>            </properties>        </profile>    </profiles>
这里同样是配置了package.target的值,也就是文件的后缀,这一个也要配置,如果不配置的话使用默认的路劲打包是没问题的而如果使用非默认的路劲则打不了包!!!

所以说来说去这两个配置还是必须的……

三、打包:

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>2.1.1</version> <configuration>                    <archive>                        <addMavenDescriptor>false</addMavenDescriptor>                    </archive>                    <!--动态定义war包名称,其中的${package.target}就是传过来的值,默认使用开始properties 中定义的package.target的值 -->                    <warName>sas_bdwallet_${package.target}</warName>                    <!--就是这里实现了加载不同目录下的配置文件和工程目录结构红框中的路劲是一样的  -->                    <webResources>                        <resource>                            <directory>src/main/resources_${package.target}</directory>                            <targetPath>WEB-INF/classes</targetPath>                            <!--使用传递过来的参数 -->                            <filtering>true</filtering>                        </resource>                    </webResources>                    <!--工程下web.xml路劲,不管是什么打包这个都要配置不然会报错  -->                    <webXml>WebRoot\WEB-INF\web.xml</webXml>                    <!--js,css,jsp路劲配置,如果不添加则js、css、jsp都不在war包中  -->                    <warSourceDirectory>WebRoot</warSourceDirectory>                </configuration></plugin></plugins></build>

注释说的很清楚了,不说太多,但是要注意<webResources>中的<resource>中的<directory>这个标签的路劲就是我们配置文件的路劲,后缀就是我们文件中配置的默认路劲或者运行时传过来的值,这样完整的路劲匹配到我们的那个路劲就加载那个文件下的配置文件……

注意:这里的webResources 这个标签,因为上一篇文章中是直接<resources> 标签下的<resource>而这里是<webResources> 标签下的<resource>所以你应该知道了为什么一个是修改上面红框中的路径一个是修改下面红框中的路劲了……

到这里我们只能使用文件中配置的那个路劲来打包,也就是如果默认配置的是test(测试环境)那就是加载了resources_test这个目录下的配置文件,如果配置的是online则加载的是resources_online这个目录下的配置文件这样相比之前方便了很多但是也有点不爽,比如我天天打测试包比较多,而打正式包时要改打完还要改回来,不太方便下面我们看看run时传递参数的方式。

四、run config 传递参数



看图片很清除了,配置Goals的值,-P(p大写)后面是你传的参数,像我们工程中的profiles的profile也就配了test 和online两个值,所以这里-p后面的参数也就只能从这两个参数中选择,其实也就是resources_test、resources_online这个后缀的动态传值。这样就ok了你传test war包就是测试的配置文件 传online war包就是正式的配置文件!

到此所有的都ok了……

五、项目中这个pom.xml配置文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.baidu.wallet</groupId><artifactId>sas_bdwallet</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>sas_bdwallet</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--配置默认值--> <package.target>test</package.target></properties><!-- 项目所依赖的jar包 --><dependencies><!-- spring springmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.2.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.2.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.0.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.6</version></dependency><!-- mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.3.0</version></dependency><!-- mybatis 链接spring包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.2.3</version></dependency><!-- mysql 驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.36</version></dependency><!--druid数据库连接池  --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.15</version></dependency><dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.9.12</version></dependency><!--文件上传  --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><!-- servlet --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><!--jackson--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.6.1</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.6.1</version></dependency><!--json --><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></dependency><!--log --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.2</version></dependency><!-- jsp c标签引入 --><dependency><groupId>org.apache.taglibs</groupId><artifactId>taglibs-standard-spec</artifactId><version>1.2.1</version></dependency><dependency><groupId>org.apache.taglibs</groupId><artifactId>taglibs-standard-impl</artifactId><version>1.2.1</version></dependency><!--AES MD5加密编码 --><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.4</version></dependency><!-- httpclient --> <dependency>            <groupId>commons-logging</groupId>            <artifactId>commons-logging</artifactId>            <version>1.1.1</version>        </dependency>          <dependency>            <groupId>commons-httpclient</groupId>            <artifactId>commons-httpclient</artifactId>            <version>3.0.1</version>        </dependency><!--测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><!--profile配置 --><profiles>        <profile>            <id>test</id>            <properties>                <package.target>test</package.target>            </properties>        </profile>        <profile>            <id>online</id>            <properties>                <package.target>online</package.target>            </properties>        </profile>    </profiles>    <build><plugins>    <!--编译插件配置-->    <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.7</source><target>1.7</target></configuration></plugin><!--打war包插件配置--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>2.1.1</version> <configuration>                    <archive>                        <addMavenDescriptor>false</addMavenDescriptor>                    </archive>                    <!--动态定义war包名称,其中的${package.target}就是传过来的值,默认使用开始properties 中定义的package.target的值 -->                    <warName>sas_bdwallet_${package.target}</warName>                    <!--就是这里实现了加载不同目录下的配置文件和工程目录结构红框中的路劲是一样的  -->                    <webResources>                        <resource>                            <directory>src/main/resources_${package.target}</directory>                            <targetPath>WEB-INF/classes</targetPath>                            <!--使用传递过来的参数 -->                            <filtering>true</filtering>                        </resource>                    </webResources>                    <!--工程下web.xml路劲,不管是什么打包这个都要配置不然会报错  -->                    <webXml>WebRoot\WEB-INF\web.xml</webXml>                    <!--js,css,jsp路劲配置,如果不添加则js、css、jsp都不在war包中  -->                    <warSourceDirectory>WebRoot</warSourceDirectory>                </configuration></plugin></plugins></build></project>


0 0
原创粉丝点击