推荐阅读:
使用Jenkins配置Git+Maven的自动化构建
在一个多开发和生产环境的项目中,有dev、beta、prod等环境设置,如下:
pom.xml文件内容如下,指定plugins和profiles等:
<build>
<finalName>dlt-serverfinalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-deploy-pluginartifactId>
<version>2.7version>
<configuration>
<skip>trueskip>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.7.1version>
<configuration>
<skipTests>trueskipTests>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<version>2.1.1version>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
<profiles>
<profile>
<id>devid>
<build>
<resources>
<resource>
<directory>src/main/resources.devdirectory>
resource>
<resource>
<directory>src/main/resourcesdirectory>
resource>
resources>
build>
profile>
<profile>
<id>prodid>
<build>
<resources>
<resource>
<directory>src/main/resources.proddirectory>
resource>
<resource>
<directory>src/main/resourcesdirectory>
resource>
resources>
build>
profile>
<profile>
<id>betaid>
<build>
<resources>
<resource>
<directory>src/main/resources.betadirectory>
resource>
<resource>
<directory>src/main/resourcesdirectory>
resource>
resources>
build>
profile>
<profile>
<id>betbid>
<build>
<resources>
<resource>
<directory>src/main/resources.betbdirectory>
resource>
<resource>
<directory>src/main/resourcesdirectory>
resource>
resources>
build>
profile>
profiles>
resource、resource.beat、resource.dev、resource.prod分别代表了不用环境的参数设置,我们在本地打包部署的时候,可以选择以下命令进行打包编译:
1、编译跳过测试:
mvn clean install -Dmaven.test.skip=true
2、部署到远程Nexus:
mvn deploy -Dmaven.test.skip=true
3、打包编译为不同的环境
mvn clean install -Pdev -Dmaven.test.skip=true
如果我们在使用Jenkins的时候不指定特定的参数的话,Jenkins默认只会根据resource
的环境参数进行编译打包,而对于特定环境dev、beta、prod这些环境变量并不会编译打包。
那么我们在使用Jenkins的时候如何设置这些参数,使得按着我们的想法去编译执行哪?答案如下:
在Jenkins ver. 2.46.1
下是这样设置的:
其中Goals and options
指定的就是以何种命令进行编译,注意的是这里是没有 ‘mvn’的,这一点在红色圈的位置也有说明,如果自己不知道加上了‘mvn’也就是参数为:
mvn clean install -Pdev -Dmaven.test.skip=true
就会报错:
[ERROR] Unknown lifecycle phase "mvn". You must specify a valid lifecycle phase or a goal in the format : or : [: ]: . Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
因此,使用正确的参数之后就可以正确的编译了~