Using Jenkins to publish tar.gz to Nexus - maven

This is problem I am trying to solve:
checkout code from Github to a local directory D
run configure command inside directory D
create a tar.gz for directory
upload taz.gz file to Nexus
I am stuck at step 3:
- I can specify the version in Maven pom.xml file, but is there a way to automatically create a build version every time Jenkins is run?
- If I specify tar.gz in pom.xml file, I would get: Unknown packaging: gz # line 6, column 13
If I specify jar inside packaging, there is no error, and files are upload to Nexus successfully.
Any advice would help, thanks!
==
follow suggestion, I am using Assembly Plugin, but still having trouble create tar.gz for Directory RE
Here is my pom.xml:
<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.o$
<modelVersion>4.0.0</modelVersion>
<groupId>Auc</groupId>
<artifactId>RE</artifactId>
<version>1.0.0.112</version>
<!-- <packaging>tgz</packaging> -->
<name>RE Repository</name>
<url>http://nexus1.ccorp.com/nexus</url>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<configuration>
<descriptors>
<descriptor>format.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Here is my format.xml file, RE directory is where I checked out the code and want to create tar.gz for it
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bundle</id>
<formats>
<format>tar.gz</format>
</formats>
<moduleSets>
<moduleSet>
<sources>
<fileSets>
<fileSet>
<directory>/var/lib/jenkins/jobs/nightly_build/workspace/RE</directory>
</fileSet>
</fileSets>
</sources>
</moduleSet>
</moduleSets>
<includeBaseDirectory>false</includeBaseDirectory>

Here is what we end up with:
mvn deploy:deploy-file -DgroupId=Home -DartifactId=RE -Dversion=0.0.0.1-SNAPSHOT -Dpackaging=tar.gz -DrepositoryId=Auc -Durl=http://nexus1.ccorp.com/nexus/content/repositories/snapshots -Dfile=RE-0.0.0.1-SNAPSHOT.tar.gz
Make sure -DrepositoryId=Auc, Auc is the deployment id you set in your setting.xml
<server>
<id>Auc</id>
<username>deployment</username>
<password>deployment123</password>
</server>

You have to use the Maven Assembly Plugin to create the tar.gz and then you can deploy it as usual with
mvn clean deploy
and the right settings.xml available on Jenkins with credentails as needed for the deployment.

Related

Packaging into a tar file in maven without creating a jar file

I'm trying to package a text based file into .tar using maven. To achieve this I used an assembly plugin and it worked, but along with the file tar a jar is also being generated. How can I avoid that?
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>all</id>
<formats>
<format>tar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>configuration</directory>
<fileMode>0444</fileMode>
</fileSet>
</fileSets>
</assembly>
You can change the packaging of your project.
I guess current packaging is jar, and thus the creation of a jar.
You may use pom and configure the assembly plugin to attach its result (the tar) to your build.
You could also configure the jar plugin, to skip the creation of empty jar (if it is your case).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<skipIfEmpty>true</skipIfEmpty>
</configuration>
</plugin>

want different maven project structure

Trying to understand maven flow.Need to create a folder called 'my-source' and copy multiple files in the same folder.
And i want a maven build zip file which should contain 'my-source' folder and its contents.How do i do that?Where ever articles i refer am seeing only java project/folder structure examples.
Also i have seen sourcedirectory/outputdirectory. Not sure where i can modify these values as i couldnot find location of effectivepom
So please give me the process to fulfil my requirement
In order to create a zip file with all your contents you will have to use the maven assembly plugin in your pom.xml. Apart from this, you will need one more file (say bin.xml) that will tell which all files you want to be packaged in the zip. The assembly plugin in the pom.xml will simply point to the bin.xml file.
See a sample code for pom.xml below.
<?xml version="1.0" encoding="UTF-8"?>
<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.abc</groupId>
<artifactId>xyz</artifactId>
<version>3.1.3</version>
<build>
<plugins>
<plugin> <!-- Create a zip file with the contents you want deployed -->
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptors>
<descriptor>src/assembly/bin.xml</descriptor> // very important. This is where we will tell where our bin.xml is located
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now create a folder structure like src/assembly and add a file with the name bin.xml. See below for a sample code that should go inside bin.xml.
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>distrib</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/my-source</directory> // the folder that you want to the packaged in the zip file
<outputDirectory>/</outputDirectory> // the location within the zip file where your folder should be copied. In this case it will place the my-source directory at the root level of the zip file.
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>

Maven - using assembly plugin in multi-module project to create distribution file

I'm using Maven to build a multi module project;
which comprises of 9 war modules
Each module has its own POM containing instructions on how to package;
<packaging>war</packaging>
A parent POM is then responsible for initiating the Maven lifecycle on each module and pushing to an artifact manager (Nexus)
<modules>
<module>module1</module>
<module>module2....
I would like to use the assembly plugin to package each of the WAR files which were built earlier by each module into a single ZIP file. I'm attempting to do this by defining an assembly descriptor in the parent POM which defines a separate dependencySet for each of the modules (using groupID, artifactID and version to find the WAR in my local repo).
I've tried to achieve this using the following assembly file;
<assembly>
<id>war</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<includes>
<include>com:test-web:war:${pom.version}</include>
</includes>
<outputFileNameMapping>test-web.war</outputFileNameMapping>
</dependencySet>
</dependencySets>
</assembly>
And this is the plugin configuration in my parent POM;
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<id>distribution-package</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<runOnlyAtExecutionRoot>true</runOnlyAtExecutionRoot>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
</configuration>
</execution>
</executions>
</plugin>
Is this approach on the right track?
Additionally does it make sense to pull the WAR files from my local repo? Should i be using the output in each of the sub modules target folders instead?
1) In your parent pom I would go like
<modules>
<module>module1</module>
<module>distibution</module>
<module>module2....
(create an extra module for the distribution) In order to separate the parent pom from the distribution.
2) You should not be using the output in each of the sub modules target folders. Add all wars as dependencies of the distribution. In that way maven can know that it must build the distribution LAST.
3) Tip: replace ${pom.version} with ${project.version} (I think the former is deprecated if it works at all)

How to use Maven assembly plugin with multi module maven project

I am new to maven and spent ~3 days in generating the zip file with assembly plugin refering to http://www.petrikainulainen.net/programming/tips-and-tricks/creating-a-runnable-binary-distribution-with-maven-assembly-plugin/ My project is multi module, so I also referred to Managing multi-module dependencies with Maven assembly plugin
Still I have several inefficiencies. Below is assembly.xml (which I inherited from 1st link)
<assembly>
<id>bin</id>
<!-- Generates a zip package containing the needed files -->
<formats>
<format>zip</format>
</formats>
<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
<dependencySet>
<!-- Project artifact is not copied under library directory since it is
added to the root directory of the zip package. -->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<moduleSets>
<moduleSet>
<!-- Enable access to all projects in the current multimodule build! <useAllReactorProjects>true</useAllReactorProjects> -->
<!-- Now, select which projects to include in this module-set. -->
<includes>
<include>com.XX:YY-main</include>
</includes>
<!--<binaries> <outputDirectory>YY-main/target</outputDirectory> <unpack>false</unpack>
</binaries> -->
</moduleSet>
</moduleSets>
<fileSets>
<!-- Adds startup scripts to the root directory of zip package. The startup
scripts are located to src/main/scripts directory as stated by Maven conventions. -->
<fileSet>
<directory>${project.build.scriptSourceDirectory}</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>log4j.properties</include>
</includes>
</fileSet>
<!-- adds jar package to the root directory of zip package -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*with-dependencies.jar</include>
</includes>
</fileSet>
</fileSets>
Below is the pom.xml (primary or main)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration> <descriptors>
<descriptor>YY-main/src/main/assembly/assembly.xml</descriptor>
<!-- <descriptor>DummyAssembly.xml</descriptor> -->
</descriptors>
</configuration>
</plugin>
Questions:
1)Since assembly.xml is referred in primary pom, all submodules also get zipped. How can I specify only certain submodules to be zipped, while all others get ignored. I tried to move YY-main/src/main/assembly/assembly.xml from main pom to child/submodule pom and have dummyassembly.xml in main which does nothing. But that is not working (possibly because dummyassembly.xml is missing some required lines). tried few things, but nothing seem to work
2)Also in assembly.xml (dependencyset), it is copying all the libraries to "lib" folder. How can I avoid this. again I tried few things (exclusion..) based on http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet but nothing worked.
Can some one provide me with specific statements that I should change in my pom or assembly files to address 1 and 2-thanks
The basic thing you should change is to create a separate module where you do the packaging which will look like this.
+-- root (pom.xml)
+--- mod-1 (pom.xml)
+--- mod-2 (pom.xml)
+--- mod-assembly (pom.xml)
The pom in the mod-assembly will look like this:
<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>
<parent>
<groupId>org.test.parent</groupId>
<artifactId>root</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>dist</artifactId>
<packaging>pom</packaging>
<name>Packaging Test : Distribution</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-one</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-two</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-bundles</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>proj1-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
That will solve the problem with running maven-assembly-plugin in every child and the problem with the dummy descriptor. Here you can find a real example of such structure.
Furthermore having the modules in one folder and the dependencies in an other folder you can use a assembly descriptor like this:
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<excludes>
<exclude>${project.groupId}:*:*</exclude>
</excludes>
</dependencySet>
</dependencySets>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<binaries>
<outputDirectory>modules</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
I think the post How to Assemble Multi-Module Project with Maven might answer your question with an example.
My answers to your questions:
1) You should add the Assembly plugin in the parent pom within the build and pluginManagement tags to reuse it in the modules; the post mentioned above is a good example on how to do it. You can also take a look at 8.6 Best Practise chapter, topic 8.6.2, of the online book 'Maven: The Complete Reference'.
2) The exclusion tag might be tricky. Again, take a look at 8.5 Controlling the Contents of an Assembly chapter of the online book 'Maven: The Complete Reference'. For example, the subtopic 8.5.4, about dependencySets mentions how to fine tune dependency includes and excludes for dependencySets; again, the subtopic 8.5.5, now about moduleSets, shows how to use it in that context.

tar gz file contains corrupt jar files after Maven build

I have a pom.xml and separate assembly-descriptor.xml file. The end result is a tar.gz file which contains my tomcat webapp and some jar files. When I build this on my local dev box (Mac OS 10.7 and Maven 3.0.3) the resulting tar.gz contains a valid jar file. When the build runs on our build box (Jenkins on linux server/Maven 3.0.3) and gets deployed to the production server, the jar file is nearly twice the size it should be and is corrupt. I can reproduce the doubling/corruption problem locally when I change the version of the maven assembly plugin to 2.3 or 2.4. When I set it to 2.1 or 2.2 or no version (it defaults to 2.2 beta 5) it works locally. But no matter what version I choose, the build fails during the tar gz step on the build box. (Local java version = 1.6.0_37 and build system is 1.6.0_34 but I don't think this discrepancy is to blame.)
Here is my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<encoding>UTF-8</encoding>
<finalName>${project.build.finalName}_${project.version}</finalName>
<descriptors>
<descriptor>src/main/assembly/assembly-descriptor.xml</descriptor>
</descriptors>
<attach>true</attach>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>package-tar-gz</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>...
`
Here is the assembly descriptor (assembly-descriptor.xml):
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/tomcat</directory>
<filtered>true</filtered>
<excludes>
<exclude>**/*.war</exclude>
</excludes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
Finally, another difference between the local build and the production build is that the production build uses a local repository and my local build does not. I've not given this much attention because I'm able to reproduce the jar file doubling/corruption problem locally (i.e. sans local repo issue).
Try removing true, this might be the culprit

Resources