如何正确在Hudson中使用Maven构建Job
相信大家都很有一个疑惑,就是我们在本地使用Hudson 构建Maven Job时,输入clean install 命令会发现它会去重新下载Jar包,一般情况下,我们都会在IDE和命令行调用过Maven构建,按理它应该不会去重新下载依赖才对,抱着和你同样的疑惑,我们现在去看看端倪。
我不打算在任务再调用一次clean install,而是直接使用help:effective-settings命令看看它是否调用我们的用户settings.xml文件。
在这里我先放一份我的用户settings.xml切片。
<settings>
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository -->
<localRepository>d:/.m2/repository</localRepository>
<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->
<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
<!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net,some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>1234</password>
</server>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>1234</password>
</server>
<server>
<id>nexus-public-snapshots</id>
<username>admin</username>
<password>1234</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://127.0.0.1:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
运行Maven Job ,目标输入help:effective-settings,控制台打印的切片如下:
[account-aggregator] $ D:\hudson_ci\hudson_dep_tools\apache-maven-2.2.1\bin\mvn.bat help:effective-settings
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO] Account Parent
[INFO] Account Email
[INFO] Account Persist
[INFO] Account Aggregator
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Account Aggregator
[INFO] task-segment: [help:effective-settings] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [help:effective-settings {execution: default-cli}]
[INFO]
Effective user-specific configuration settings:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== -->
<!-- -->
<!-- Generated by Maven Help Plugin on 2010-12-08T11:24:17 -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/ -->
<!-- -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!-- -->
<!-- Effective Settings for 'JDONEE-ZAH$' on 'Jdonee-ZAH' -->
<!-- -->
<!-- ====================================================================== -->
<settings xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository xmlns="http://maven.apache.org/SETTINGS/1.0.0">C:\Windows\System32\config\systemprofile\.m2\repository</localRepository>
</settings>
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Wed Dec 08 23:24:17 CST 2010
[INFO] Final Memory: 6M/127M
[INFO] ------------------------------------------------------------------------
Finished: SUCCESS
God,怎么把构件都下载C:\Windows\System32\config\systemprofile\.m2\repository这个位置了。
看来Hudson根本就不认可Maven的环境变量以及用户级settings.xml。
还好有神奇的-s参数(--settings)帮忙,这个参数的作用就是让用户指定可运行的settings.xml文件。我的用户级settings目录存放在C:\Users\Jdonee\.m2目录下。
那么我们目标改成help:effactive-settings -s C:\Users\Jdonee\.m2\settings.xml,马上查看控制台的打印切片:
[account-aggregator] $ D:\hudson_ci\hudson_dep_tools\apache-maven-2.2.1\bin\mvn.bat help:effective-settings -s C:\Users\Jdonee\.m2\settings.xml
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO] Account Parent
[INFO] Account Email
[INFO] Account Persist
[INFO] Account Aggregator
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Account Aggregator
[INFO] task-segment: [help:effective-settings] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [help:effective-settings {execution: default-cli}]
[INFO]
Effective user-specific configuration settings:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== -->
<!-- -->
<!-- Generated by Maven Help Plugin on 2010-12-08T11:26:55 -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/ -->
<!-- -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!-- -->
<!-- Effective Settings for 'JDONEE-ZAH$' on 'Jdonee-ZAH' -->
<!-- -->
<!-- ====================================================================== -->
<settings xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository xmlns="http://maven.apache.org/SETTINGS/1.0.0">d:/.m2/repository</localRepository>
<servers xmlns="http://maven.apache.org/SETTINGS/1.0.0">
<server>
<username>admin</username>
<password>***</password>
<id>nexus</id>
</server>
<server>
<username>admin</username>
<password>***</password>
<id>nexus-releases</id>
</server>
<server>
<username>admin</username>
<password>***</password>
<id>nexus-public-snapshots</id>
</server>
...省略...
哈哈,大功告成对不对,可是现在还有点小小的瑕疵,如果我定义很多的Maven任务,新建任务都要再输一遍实在是很麻烦(虽然很多时候,我们会通过复制任务减少一些配置量),而且一旦我迁移到其它系统环境,比如Linux等等,同样也不方便。
所以我们必须要把它定义到全局变量中。
点击“系统管理”,选择“系统设置”,启用“Environment variables”,新增一个环境变量,输入如Key=yoursettings,Value=C:\Users\Jdonee\.m2\settings.xml。
注:key值不要使用特殊字符如"-"等,以免命令行不认。
关于Environment variables的官方解释是:这些键值对每个节点上的每个应用都有效.它们可以在Hudson配置(如$key或者${key})中使用, 并且在每个构建启动时被加入到环境变量中.
好了,我们在Job的目标更改为help:effective-settings -s $yoursettings执行,控制台打印更上面的切片一样,Perfact!
Hudson 系列的文章写得够细、够全! 学习了…
Jdonee 探索精神不错,学习一下。 :D help:effactive-settings -s $yoursettings 又学了一个新用法。