How can I build to repository sub project using Jenkins - maven

I have Git repository in which one repository contains two project as below.
Repository
- Project A
- Project B
My question is how can I build single project (Project A) through Jenkins.
When I use to git clone SSH URL in jenkinsfile it builds to whole repository but I want to build only Project A. So, could you please help me how can I do the same?
Please check below my jenkinsfile code:
node {
stage ('Build') {
git url: 'git#github.com:abc/sample.git'
withMaven {
sh "mvn clean install"
}
}
}

Is not a best practice to have several apps or libraries in one git repository. I advise you to use a git repository for each project. Also don't use spaces in sub projects name.
According to your comments, you will be able to compile your artifacts with one of the follow approaches:
Parent pom
As a extremely summary, you could build several maven projects with one hit. In your case you must add one pom.xml at root.
my-git-repository
├── pom.xml
├── project-a
├── pom.xml
├── project-b
├── pom.xml
This new pom.xml must be something like this:
?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>acme.foo</groupId>
<artifactId>bar</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<dependencies>
...
</dependencies>
<modules>
<module>../project-a</module>
<module>../project-b</module>
</modules>
<!-- Build -->
<build>
...
</build>
</project>
Finally, at root execute mvn clean package. As you will see, all the registered project in <modules> will be compiled.
More details about maven submodules here:
https://maven.apache.org/guides/mini/guide-multiple-modules.html
dir step
You are using node in your script, so it's not a declarative pipeline in which jenkinsfile is used. You are using a scripted pipeline
No matter if you are using scripted pipeline or declarative pipeline, a step called dir is the solution
node {
stage('Build') {
git url: 'git#github.com:abc/sample.git'
dir("Project-A") {
withMaven {
sh "mvn clean package"
}
}
dir("Project-B") {
withMaven {
sh "mvn clean package"
}
}
}
}
More details about dir step and jenkins pipelines type here:
https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/
search dir: Change current directory section
Jenkins scripted pipeline or declarative pipeline

Related

Build multi maven modules then copy JARs inside a docker image

I have a multi-modules project configured with maven.
/myProject
/module1
/target
pom.xml
/module2
/target
pom.xml
pom.xml
The pom parent is building the submodules.
Using the com.spotify.dockerfile-maven-plugin I want to build all the modules, build a docker image and copy all the JARs inside that image.
The pom parent
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>myTest</artifactId>
<name>test</name>
<version>0.0.1</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
</project>
If I put a Dockerfile at the root of /myProject, the plugin complains there is no Dockerfile for module1
FROM openjdk:8-jdk-alpine
ARG JAR_FILE
COPY ${JAR_FILE} /home/xyz/jars
maven plugin:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<configuration>
<repository>mytest</repository>
<buildArgs>
<JAR_FILE>target/${project.artifactId}-${project.version}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
This works fine with one module if I put the Dockerfile inside that module but how to do with multiple modules and only 1 Dockerfile?
Can I build the submodules and then the docker image with a single mvn clean install on the parent pom?
Maybe by creating a submodule only with a pom and the Dockerfile?
If the jars are dependencies for one executable then yes, you could just build that one executable with its dependencies declared in its pom.xml file. Maven should figure out which order to build the submodules in, so long as you don't create a cycle. As you suggest, you want to make it a submodule and not the root pom as the root pom is parent configuration that is inherited by other modules. Just put the plugin on that one submodule (in its pom.xml) and only give it the Dockerfile.
(If you think of this as a problem about having one executable depending on libraries in the same maven project then it's not really related to docker. You could ask the same question about a spring boot app using libraries in the same multi-module project. In a sense it just is that problem as the jar will get built together with its dependencies and copied into the docker image.)
Or if you mean that each jar for each submodule is an executable in itself (and I don't think you do) then you could create multiple docker images, each from its own Dockerfile, and then you can bundle and start them together if you want to with a docker-compose file.

version property dynamic in pom.xml

I have a Maven pom.xml, I build project and executable jar deploy in Nexus with Jenkins.
But I want changes of version name according to branch name.
For example: I have in pom.xml
<groupId>net.test</groupId>
<artifactId>test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>iin-parent</name>
<packaging>pom</packaging>
I need set like this : (Branch- Master/Test1/Test2/..)
<groupId>net.test</groupId>
<artifactId>test-parent</artifactId>
<version>BranchName_0.0.1-SNAPSHOT</version>
<name>iin-parent</name>
<packaging>pom</packaging>
How can this be done?
I was using MVN build like -Drevision=BranchName-SNAPSHOT clean compile package deploy. But I want dynamically fetch the branch name.
enter code here
If you use clean compile package deploy you are duplicating several parts..only clean deploy is needed. Please read the documentation about Maven Life cycle.
Furthermore if you like to change the version dynamically you can do that by using the correct properties (Starting with Maven 3.5.0+) which are ${revision}, ${sha1} and ${changelist} so for example:
<groupId>net.test</groupId>
<artifactId>test-parent</artifactId>
<version>${revision}</version>
<properties>
<revision>1.0-SNAPSHOT</revision>
</properties>
This can be done in Maven like this:
mvn -Drevision=2.0-SNAPSHOT clean package
or if you like to do this for a branch:
mvn -Drevision=2.0-BranchName-SNAPSHOT clean package
You have to be aware if you like to do mvn clean deploy please read carefully the docs and follow them.

how to deploy artifacts (jar files) from POM.xml into jFrog repository?

I am new to this repository world.
I have a maven project i.e MavenExample from GitHub. and I have installed a jFrog artifactory in my machine.
My Aim is to deploy all the jar files listed in my pom.xml into jFrog artifactory instead of .m2 repo (default). This deployment of jar file must ocucur after mvn deploy command.
I have tried adding distributionManagement inside Pom.xml and changing the settings.xml inside maven/conf.
Can someone help... much appreciated.
Edited:
You can try editing "localRepository" in your settings.xml":
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<!--
REST OF THE CODE
-->
</settings>
Go to your project directory and launch "mvn deploy".
The easiest way to obtain this information is from the "Set Me Up" section of Artifactory. Select the repository in the Artifacts tab, and in the top right corner, click "Set Me Up". Assuming you are using a local repository for deploying (remote will not work for deploying, only for resolving dependencies), it will show a section for deploying which will include the distribution management section. Place this in the pom.xml file of the parent project and this running the mvn deploy command from this directory will deploy to Artifactory.
A great way to see a working example of this is to view the "maven-example" section in JFrog's public Github page . You can see the parent project has 3 sub-modules (multi1, multi2, and multi3). Adding the distribution management section mentioned previously to the parent pom.xml file and then running mvn deploy from the parent project's root directory will deploy all the binaries to Artifactory.
If for some reason this is still not working, please provide your pom.xml for the parent, the name, package type, and if it is a local/remote/virtual of the repository. Additionally, please provide any output of the mvn deploy command (with the -X option) and anything you can find in the artifactory.log and request.log files ($ARTIFACTORY_HOME/logs/artifactory.log and request.log)

build multiple modules multiple times in parent pom

There are several applications that need to be built and packaged from a number of modules.
In parent pom, i'm using the profile to invoke builds for different apps.
root
parent/
pom.xml
moduleA/
pom.xml
moduleB/
pom.xml
moduleC/
pom.xml
For example, app "profile-1" would need a subset of existing modules to be built and put together as a tar ball.
The tar would contain several jars and different config files pulled from the target/ of the sub modules.
I'm using a shell script invoked using exec-maven-plugin to put together the tar.
The problem I'm facing is that, in one application, i need to build the same module multiple times but with different maven parameters.
What is the best way to do this?
<profiles>
<profile>
<id>profile-1</id>
<modules>
<module>../moduleA</module>
<module>../moduleB</module>
<!-- <module>../moduleC</module> -->
</modules>
<properties>
<global.version>0.0.1-SNAPSHOT</global.version>
</properties>
<build>
<!-- use maven exec plugin to run a shell script which generates the tar ball picking jars and config files from different modules target dirs -->
<plugins>
</plugins>
</build>
<profile>
</profiles>
A sample sub module pom
<groupId>com.test</groupId>
<artifactId>moduleC</artifactId>
<packaging>bundle</packaging>
<version>${global.version}</version>
<name>test :: ${project.artifactId} :: ${name} </name>
<parent>
<groupId>com.test</groupId>
<artifactId>parent</artifactId>
<version>${global.version}</version>
<relativePath>../parent</relativePath>
</parent>
Things i tried:
1) Can i separate into multiple profiles and invoke them as -Pprofile-1,profile-2?
It did not work for me but i would be doing something wrong.
2) Have another shell script that has mvn command line to build the moduleC in different ways.
- Even though i pass in the "-Dglobal_version", the moduleC run from mvn command line does not seem to find the parent in the repository.
I tried doing a "-N" build to put the parent pom in the repository before building the application but did not help.
Best way is:
mvn clean install --projects moduleA, moduleB
mvn clean install --projects moduleB, moduleC
You can't run multiple builds with maven (see this stackoverflow question)

hudson incremental maven build always fail, while full maven build succeeds

Upon each change commited to our svn, hudson initiates a maven build with the -amd -pl flags, to make only the changed projects. However, the project it compiles "a" is dependent on another project "b", and it fails while looking for "b" in maven repositories across the web. Half an hour later it does a full build and succeeds...
Maybe we've set up our maven dependencies wrong? We have several projects a,b,c and one "maven-parent" project who has only a pom.xml with this in it:
<project>
<artifactId>maven-parent</artifactId>
<packaging>pom</packaging>
<modules>
<module>../a</module>
<module>../b</module>
<module>../c</module>
</modules>
</project>
and the "a" project references "b" like so:
<project>
<artifactId>a</artifactId>
<packaging>jar</packaging>
...
<dependency>
<groupId>com.pursway</groupId>
<artifactId>plummet</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</project>
Thanks!
Set up each project as a separate project in Hudson and use the Hudson configuration for downstream dependant projects to build whatever is necessary depending on the scm changes.
Perhaps you should try -am -pl. From mvn --help
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
You can specify what Raguram has pointed out in hudson project configuration. Under the build
option you can specify Maven Goals and options.
See that in image below
http://imageshack.us/photo/my-images/696/hudsonmaven.jpg/

Resources