In Maven, can I pack JEE project in Zip archive together SQL scripts? - maven

Hello everyone first of all,
Suppose I have a JEE project contents some modules and a 'sql' folder inside:
I build all modules to a 'integration-version.ear' archive file.
There is the content of my target:
Using Maven, can I build something like this:
There is the ‘3.15’ folder includes some SQL files from the same folder of my project.
Is it possible? What is configuration of Maven needed?
Note:
The original question was like: "In Maven, can I pack JEE project in RAR archive together SQL scripts?" but now the question is changed because RAR is a proprietary format and each tools that create RAR files must be commercially licensed.
Solution:
Using build-helper-maven-plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>parse-version</goal>
</goals>
<configuration>
<propertyPrefix>parsedVersion</propertyPrefix>
</configuration>
</execution>
...
Using maven-assembly-plugin
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<inherited>false</inherited>
<configuration>
<descriptors>
<descriptor>${project.basedir}/../assembly.xml</descriptor>
</descriptors>
</configuration>
...
assembly.xml
<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>release</id>
<baseDirectory>${file.separator}</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}/../sql/migrates/${parsedVersion.majorVersion}.${parsedVersion.minorVersion}</directory>
<outputDirectory>/scripts-${parsedVersion.majorVersion}.${parsedVersion.minorVersion}</outputDirectory>
<includes>
<include>**/*.sql</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputFileNameMapping>${build.finalName}.${packaging}</outputFileNameMappin>
<includes>
<include>*:ear</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>

There are 2 Maven plugins which can do that: maven-assembly-plugin and maven-rar-plugin
If you want to use maven-rar-plugin, i would create a new module with packaging rar and configure the plugin.
I personally think maven-assembly-plugin is easier to use.

Related

How deploy a zip file whith Maven assembly plugin in the artifactory referentiel

I develop a web application that has two layers: a frontend layer and a backend. I deployed the backend war in artifactory using distributionManagement. Now I want to deploy the frontend. I use maven assembly plugin. I made a zip. I would like to know how to deploy the frontend zip in artifactory? Thank you for your help.
<!-- Maven assembly plugin -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>myArchive</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/zip.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
<!--My zip.xml-->
<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>WebContent</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>WebContent</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
You can use maven command to deploy separate files:
mvn -X clean package deploy:deploy-file -Dfile=/frontend-1.0.zip

Assembly plugin generating 2 jars. One with classfiles. One with dependencies

I am having trouble building a jar using the assembly plugin. I have a bunch of system jar files that I want bundled alongside class files of the project.
I was looking to do something like:
final
- lib
- system.jar
- system1.jar
- com
- servlet
-- etc
I have tried to use the assembly plugin but it generates 2 jars. One with just the lib with my jars and another with the class files.
I have read through the assembly docs but I must be missing something.
Here is the build element I have in my pom.xml
<dependency>
<groupId>jackson-mapper-asl</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jackson-mapper-asl-1.9.4-sc1.jar</systemPath>
</dependency>
//... etc
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>final-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This is my assembly file (assembly.xml)
<assembly>
<id>distribution</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<scope>system</scope>
<outputDirectory>lib</outputDirectory>
<includes />
</dependencySet>
</dependencySets>
</assembly>
Any idea how I can merge these jars together?
You would usually have a much simpler layout, such as:
final
- lib
-system.jar
-system1.jar
-project.jar
I.e. the project itself is a jar too, and included in the lib. In this case just add <useProjectArtifact>true/>useProjectArtifact> to the dependencySet.
If you really, really want to have classes copied in directly, try adding a fileset to the assembly, copying from target/classes into your root location
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>

maven copy-dependencies includeGroupIds and all transitive elements to those

I try to figure out how to copy all dependencies to an explicit version and all the required dependencies.
For Example: My project requires version 3 of a third party lib, called foobar.
I want to copy the version 3 libraries to a folder named lib-foobar-${foobar.version}.
In this folder are those jars which are required to use foobar in version 3. That means the jar itself and all dependent jars which are declared in the foobar pom.
I currently use the org.codehaus.mojo:maven-dependency-plugin:2.1 with goal copy-dependencies in phase package.
My configuration is:
<configuration>
<outputDirectory>${project.build.directory}/lib-foobar-${foobar.version}</outputDirectory>
<includeGroupIds>com.foobar</includeGroupIds>
<excludeTransitive>false</excludeTransitive>
<excludeScope>test</excludeScope>
<includeScope>compile</includeScope>
</configuration>
I don't want to list all allowed and not allowed lib's because a step to a newer version takes place every month.
Are there any other tools which can do that or is there any dodge for that?
Big thanks to user944849.
It was very helpful to me for finding out the best solution in that case.
For everyone who is interested in my solution, here it comes:
At first I changed the plugin to maven-assemby-plugin and added a new assembly file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<dependencies>
<dependency>
<groupId>com.foobar</groupId>
<artifactId>foobar-parent</artifactId>
<version>${foobar.version}</version>
<type>pom</type>
</dependency>
</dependencies>
<executions>
<execution>
<id>copy-foobar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assemble/foobar-libs.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
And the assembly file looks like:
<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>standalone</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>org.foobar</include>
</includes>
<outputDirectory>/lib-foobar-${foobar.version}</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<useTransitiveFiltering>true</useTransitiveFiltering>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
The key was the <useTransitiveFiltering> element, which resolves all transitive libs based on the included libs.

Maven - Using the Assembly plugin to package dependencies from multiple projects into one file

I’m working on a project that is made up of multiple other projects. It has the following structure:
MainProject
--projectA
--projectB
--projectC
--projectD
--parent
--projectE
...
--projectX
As you can see the parent is amongst the others. I’m not sure if this is unusual or not but it is the way it is and I have to just work with it.
I’m looking to use the Assembly plugin to get the dependencies from each of the sub projects and zip them up all within one folder. So for example, I want a zip file containing a folder which contains all of the unpacked dependencies of the children projects.
This is what I have so far:
assembly.xml (this is in the parent project)
[...]
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<includes>
<include>com.example:../one</include>
<include>com.example2:../two</include>
<include>com.exampleX:../three</include>
</includes>
<binaries>
<outputDirectory>/library/jars</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<outputDirectory>library/jars</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<excludes>
<exclude>**.xml</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
and this is in the parent pom.xml
[...]
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
[...]
When I run a mvn clean install from cmd the build is successful. It does not however create a zip file containing my library/jars directory which should then contain all of the dependencies from each of the projects. It simply creates zips in each project. These zips do not contain the directory I want or any dependencies, they are simply zips of the each project directory. Can anyone see what I am doing wrong?

Maven: creating an archive (zip or jar) file containing files outside the project?

I'm currently working on a Maven based project in which I have unit tests that generate PDF files in, let's say "C:/result" directory (I definitely don't want these files to be created within any directory from my project).
Using the maven-assembly plugin, would it be possible to create an archive file (preferably zip or jar) that gathers the files that are thus generated in "C:/result"?
Basically, my goal would be to upload this archive as a regular artefact onto a Nexus server, every time I deploy a new version of my project.
Thx in advance.
You'd need to define an assembly descriptor similar to:
<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>
<formats>
<format>zip</format>
</formats>
<baseDirectory>C:/</baseDirectory>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>results</directory>
<outputDirectory>./doc</outputDirectory>
</fileSet>
</fileSets>
</assembly>
Save e.g. as src/assembly/bin.xml, it will be referred from the pom.
The pom would contain in the build section:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>out</id>
<configuration>
<descriptors>
<descriptor>src/assembly/bin.xml</descriptor>
</descriptors>
</configuration>
<phase>verify</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
see for more information maven assembly documentation.

Resources