一:浩言
细水长流,岁月静好,需要一颗不折腾的心,更需要一颗明白的心。每个人的内心都需要一个空间。
<h5 align = "right">----------------《不畏将来不恋过去》</h5>
二:背景
年底了,我的工作终于开始收尾了,从12月份吧开始重构项目,到现在一直在忙着迁移和新做接口,还有其他项目的各种问题需要跟进。最近API接口进行收尾工作,可是开完最后的接口每次处理配置文件好麻烦,每次上测试生产都是要注释,然后在注释回去,近期没有那么忙了,就开始把这个问题都给解决了。
直接上配置说明了。尝试了两种方式,个人比较推荐第一种
2.1 通过profiles来指定环境配置如下:
Paste_Image.png
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testProfile</groupId>
<artifactId>testProfile</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>testProfile</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>test</id>
<properties>
<package.environment>test</package.environment>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<package.environment>production</package.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>development</id>
<properties>
<package.environment>development</package.environment>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>test/**</exclude>
<exclude>production/**</exclude>
<exclude>development/**</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/${package.environment}</directory>
</resource>
</resources>
<finalName>testProfile</finalName>
</build>
</project>
直接执行install得到的classes文件目录如下
Paste_Image.png
实际中development, production,test中的配置文件名字都一样,我这里只是为了看的更加明显而已。
有一点要说明的是我在弄的时候一直以为目录会是这样,比如我现在默认指定的是生产的,我以为打包后的目录就是/production/sys_conf_pro.properties,实际上resource中的配置了excludes节点已经排除了,所以不会再有这一个目录。所以如果你的配置文件src/main/resources下的目录时conf/production/sys_conf_pro.properties想做到conf/sys_conf_pro.properties也就是只想排除production目录是做不到的。
命令行打包命令:mvn install -Dmaven.test.skip=true -Ptest
-P:是用来指定参数。
-D是用来跳过单元测试。
2.2 将配置属性配置在profile 里的properties中:
配置如下,我只配置了test的数据,同时需要配置build节点中的resources。对于项目中的properties中的使用${key}就行。
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testProfile</groupId>
<artifactId>testProfile</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>testProfile</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>test</id>
<properties>
<package.environment>test</package.environment>
<profile.http.url>http://127.0.0.1/test</profile.http.url>
<profile.sms.url>http://127.0.0.1/sms</profile.sms.url>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<package.environment>production</package.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>development</id>
<properties>
<package.environment>development</package.environment>
</properties>
</profile>
</profiles>
<build>
<finalName>testProfile</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
Paste_Image.png
使用工具编译:
Paste_Image.png
编译后的结果:
Paste_Image.png
其实resource中还有很多属性,可以根据自己的需要进行配置。
三:制作bat文件
按照下图的方式制作bat运行
bat_one_Image.png
bat_two_Image.png
bat_three_Image.png
bat_four_Image.png
得到上面的Commond Line然后在本地文件夹建立一个*.bat,把这段复制进去即可。
java -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.7.0_79\bin\javaw.exe" -
Dmaven.home=EMBEDDED -
Dclassworlds.conf=D:\wuhaoWorkSpace\MyeclipseSpace\.metadata\.plugins\org.eclipse.m2e.launching\lau
nches\m2conf686032403387202491.tmp -
Dmaven.bootclasspath=/E:/studySoft/MyEclipse/plugins/org.eclipse.m2e.maven.runtime_1.4.0.20130531-
2315/jars/plexus-classworlds-2.4.jar -Dfile.encoding=GBK -classpath
/E:/studySoft/MyEclipse/plugins/org.eclipse.m2e.maven.runtime_1.4.0.20130531-2315/jars/plexus-
classworlds-2.4.jar org.codehaus.plexus.classworlds.launcher.Launcher -B clean
pause
最后一个pause是我自己加的,我将上面的javaw.exe改为java.exe还是闪退,所以加了一个pause后按键后才推出
而install的bat制作只是将clean的-B clean改为-B install -Ptest即可
Paste_Image.png
在执行打包的时候需要先clean,所以我的install的bat如下
@call mvn-clean.bat
java -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.7.0_79\bin\java.exe" -
Dmaven.home=EMBEDDED -
Dclassworlds.conf=D:\wuhaoWorkSpace\MyeclipseSpace\.metadata\.plugins\org.eclipse.m2e.launching\lau
nches\m2conf4230151797058056596.tmp -
Dmaven.bootclasspath=/E:/studySoft/MyEclipse/plugins/org.eclipse.m2e.maven.runtime_1.4.0.20130531-
2315/jars/plexus-classworlds-2.4.jar -Dfile.encoding=GBK -classpath
/E:/studySoft/MyEclipse/plugins/org.eclipse.m2e.maven.runtime_1.4.0.20130531-2315/jars/plexus-
classworlds-2.4.jar org.codehaus.plexus.classworlds.launcher.Launcher -B install -Ptest
pause
四、maven编译跳过junit
配置后,maven打包的时候跳过junit
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
编译并且加载lib下的jar包
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<extdirs>src\main\webapp\WEB-INF\lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
五:浩语
__
__ _ ____ __| |__ _____ ___
\ \/ \/ / | \ | \\__ \ / _ \
\ /| | / Y \/ __ \( <_> )
\/\_/ |____/|___| (____ /\____/
\/ \/
任何事情都是要靠努力和用心。