1. Use aggregation to manage multiple sub-projects
Maven has an aggregation (aggregation). You can add multiple projects as modules to a pom. XML, and then tell Jenkins the top-level pom. XML to automatically extract the source code of these projects from git, build all sub-projects, and run the corresponding test programs. I use testng here.
For example, I have two Maven projects, client and email_sender. I Want To compile and test them at a time and add a pom. xml file to the directory on the two project folders. The content is as follows:
<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.kaimei.datacenter</groupId> <artifactId>cml</artifactId> <version>2.0</version> <packaging>pom</packaging> <modules> <module>client</module> <module>email_sender</module> </modules></project>
Then, you only need to tell Jenkins the path of the Pom. xml file. Click build now to perform the test. You can see that both sub-projects have been downloaded, compiled, and run the testng test program.
Note that the module name should be the Directory Name of the subitem.
2. Dependency management between multiple projects
The client and email_sender projects are not associated in the preceding example. To demonstrate continuous integration in build management of the dependency tree, a new Maven project named pai_utility is created, this project provides a class dbmanager that encapsulates MongoDB connections and the Code comes from my other article: http://blog.csdn.net/sheismylife/article/details/7090526
Both the client project and the email_sender project use the dbmanager class to establish a connection with MongoDB. Therefore, they all have this section in POM. xml:
<dependency> <groupId>com.kaimei.datacenter</groupId> <artifactId>mongo_utility</artifactId> <version>1.0</version> </dependency>
Build successful. Taking a closer look at the console output, Jenkins's behavior is not the same as using MVN clean Compile directly. Jenkins will extract the source code of mongo_utility, recompile it, install it in the local repository, and then compile the other two projects. If MVN clean compile is directly typed, The mongo_utility-1.0.jar will download and install it directly from the private server without compiling.
3. Multiple projects share the same settings
Client, email_sender, and mongo_utility all use the MongoDB Java library. To facilitate management, it is not necessary to define the same dependency in their respective Pom. XML files. Maven regards the Pom. xml of each project as a project object and allows them to share the same configuration through inheritance.
Add the MongoDB dependency to the top-level Pom. xml file:
<dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.7.2</version> </dependency> </dependencies>
Then, delete the preceding dependency in the Pom. xml file of the client, mongo_utility, and email_sender projects, and add the following Configuration:
<parent> <groupId>com.kaimei.datacenter</groupId> <artifactId>cml</artifactId> <version>1.0</version><!-- or whatever version you use --> <relativePath>../pom.xml</relativePath> </parent>
And then test it.
4. Deploy the module on the nexus Server
If you want to deploy a module project such as the mongo_utility project to the nexus server, you need to adjust the Pom. XML in the top-level directory and add the following Configuration:
<distributionManagement> <repository> <id>sheismylife</id> <url>http://S1:8081/nexus/content/repositories/sheismylife</url> </repository> </distributionManagement>
However, all three modules automatically inherit the deploy function. In fact, I only want to deploy the mongo-utility module on the nexus. I will put the problem here for the time being.
Then execute MVN clean deploy in the top-level directory. Submit the code to the GIT repository, modify the Jenkins project configuration page, and add the clean deploy command.
Click "build now" and check the result. Everything is OK. it is worth mentioning that if you execute the command through the MVN command line correctly and run the error through Jenkins, restarting the Jenkins service may solve the problem. I have already met Jenkins several times. It seems that Jenkins has a bug.
5. Deploy the WEB Project to glassfish or Tomcat through Jenkins
The basic practice is to use the plug-in of the maven project to complete the deployment. One problem is that if the web project and several other projects are aggregated as modules of a large project, and all inherit a parent pom. XML, you can use a Maven command to compile all the modules, deploy them to the nexus private server, and release the WEB Project to glassfish or tomcat. I am still researching and hope to implement this function through Maven.
6. sequential execution between multiple projects on a Jenkins
This is very simple. In Jenkins project configuration, you can set build after a project, or build other project after the project build.
7. Remote calls between multiple Jenkins instances
Jenkins A on S1 calls a shell script, such as curl http: // S2/job/test/build
The test project of Jenkins B on S2 will be triggered.
For a complex example, Jenkins A calls jenkinsb and passes the parameter. The parameter must be in JSON format. Therefore, according to the curl syntax, it should be written as follows:
curl http://10.112.18.110:8080/jenkins/job/upland-install/build -d token=zorn --data-ascii json="{'parameter':[{'name':'number','value':${BUILD_NUMBER}}]}"
In Jenkins B, select this build is parameterized.
Set the parameter name and default value. Then reference the parameter in the form of % Number %.