I have next directory structure:
src/main/resources/export/v1/android/
src/main/resources/export/v1/ios/
src/main/resources/export/v2/android/
src/main/resources/export/v2/ios/
...
src/main/resources/export/vn/android/
src/main/resources/export/vn/ios/
And I need to obtain next result:
WEB-INF/export/v1/android.zip
WEB-INF/export/v1/ios.zip
WEB-INF/export/v2/android.zip
WEB-INF/export/v2/ios.zip
...
WEB-INF/export/vn/android.zip
WEB-INF/export/vn/ios.zip
Can I solve the problem with maven-assembly-plugin? If not, is there an another plugin that can cope with it or is it better to write custom class and call it with exec-maven-plugin?
Here's what I've figured out by tinkering with the plugin. I've been able to create one zip with some arbitrary files in it. Here's the plugin I've defined:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptors>
<descriptor>src/main/resources/my-assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>assembly-id</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
And heres my-assembly.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>zip-example</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/java/com/sandbox</directory>
</fileSet>
</fileSets>
</assembly>
I'm hoping there's an easier way to do this for your needs, but from this point you can create an assembly file for each zip you need to build and this will work for you.
I've ended up with writing java class and runnign it with exec-maven-plugin as below:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>Running custom java class</id>
<phase>compile</phase>
<goals><goal>java</goal></goals>
<inherited>false</inherited>
<configuration>
<mainClass>mycompany.CustomJavaClass</mainClass>
<classpathScope>compile</classpathScope>
<arguments>
<argument>
${project.basedir}/src/main/resources/export
</argument>
<argument>
${project.basedir}/target/art-1.0/WEB-INF/export
</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
Related
I'm using the maven-deploy-plugin to deploy a third party jar (previously downloaded to my target directory from a plugin not show here) to Nexus as the name 'third-party-1.0.jar', this all works fine using the configuration below.
I also have a javadoc directory in my target directory, which is the javadoc for this third party jar. I'd like to package that javadoc directory as 'third-party-1.0-javadoc.jar'.
If I could get the directory packaged as a JAR, I think I could the javadoc parameter of the deploy plugin below to deploy it, just unsure how to package a custom directory as a JAR with a specific name using Maven, maybe the assembly plugin?
TLDR; How do I use Maven to create a JAR file from the contents of a directory I specify?
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>none</phase>
</execution>
<execution>
<id>deploy-jar</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<file>${project.build.directory}/code.jar</file>
<url>...</url>
<repositoryId>...</repositoryId>
<url>...</url>
<packaging>jar</packaging>
<generatePom>true</generatePom>
<groupId>com.example</groupId>
<artifactId>third-party</artifactId>
<version>1.0</version>
</configuration>
</execution>
</executions>
</plugin>
You can achieve it by using maven-jar-plugin or by using maven-assembly-plugin
Here is a way to achieve it via maven-jar-plugin
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<configuration>
<classifier>docs</classifier>
<classesDirectory>${project.build.directory}/docs</classesDirectory>
<includes>**/*</includes>
</configuration>
<id>pack-docs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Now run:
mvn clean package
It will create artifact_name-1.0.0-docs.jar jar
How to create addition jar using Maven?
I ended up using the maven-assembly-plugin instead of the maven-jar-plugin as it allows more control over the resulting JAR, including not adding a pom.xml to it, not attaching it to the project as an artifact to be deployed (attach=false), and how it
is named. This allows the maven-deploy-plugin to control how it is deployed, and what Maven coordinates to use when deploying it.
I can then just attach the Javadoc JAR built with the assembly plugin into my deploy file execution using the javadoc tag:
src/main/assembly/assembly.xml
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>package-third-party-javadoc</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/dir</directory>
<outputDirectory>.</outputDirectory>
</fileSet>
</fileSets>
</assembly>
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>package-third-party-javadoc</id>
<phase>compile</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<finalName>third-party-javadoc</finalName>
<appendAssemblyId>false</appendAssemblyId>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>none</phase>
</execution>
<execution>
<id>deploy-jar</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<file>${project.build.directory}/code.jar</file>
<url>...</url>
<repositoryId>...</repositoryId>
<url>...</url>
<packaging>jar</packaging>
<generatePom>true</generatePom>
<groupId>com.example</groupId>
<artifactId>third-party</artifactId>
<version>1.0</version>
<javadoc>${project.build.directory}/third-party-javadoc.jar</javadoc>
</configuration>
</execution>
</executions>
</plugin>
I am trying to create a tar.gz with the name my-project-2.0.tar.gz and inside that file there is a zip file with all my java classes called my-project.zip. The reason for this is due to some script files that look for this type of format.
I have set up the maven assembly plug in my pom file and the descriptor files.
This issue is that when I run my build the plug in and descriptors are ignored.
The goal in eclipse is clean package.
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<executions>
<execution>
<phase>package</phase>
<id>assemble</id>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/prep.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
<execution>
<phase>package</phase>
<id>bundle</id>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>my-project-2.0</finalName>
<descriptors>
<descriptor>src/assembly/bundle.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
This is the bundle descriptor:
<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</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target</directory>
<includes>
<include>my-project.zip</include>
</includes>
<outputDirectory></outputDirectory>
</fileSet>
<fileSet>
<directory>src/assembly</directory>
<includes>
<include>place.sh</include>
</includes>
<lineEnding>unix</lineEnding>
<fileMode>0755</fileMode>
<outputDirectory></outputDirectory>
</fileSet>
</fileSets>
</assembly>
This is the prep descriptor
<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>prep-id</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory></outputDirectory>
</fileSet>
</fileSets>
</assembly>
Ive tried running clean assembly:assembly but I get a `No assembly descriptors found' error
Ive tried rewritting my plug in as:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<finalName>my-project-2.0</finalName>
<descriptors>
<descriptor>src/assembly/bundle.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<id>assemble</id>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
But I dont know how to name the inside zip as my-project.zip
When the zip file is created with prep.xml the name of the zip file should be ${project.artifactId}-${project.version}.zip.
So in your case my-project-${project.version}.zip.
Try to edit the bundle.xml the following way:
<fileSet>
<directory>target</directory>
<includes>
<include>my-project-${project.version}.zip</include>
</includes>
<outputDirectory></outputDirectory>
</fileSet>
The main reason why my build was ignoring my descriptors is because I had all my plugins wrapped around a <pluginManagement> </pluginManagement>.
This solved my issue
I have a project which is using a maven assembly plugin to include a bunch of files into a tar. Now I want to include a few more files in that tar, but first I want to zip them up and actually include that zip file.
I tried doing something like this: maven-assembly plugin - how to create nested assemblies
So basically created 2 executions inside the maven assempbly plugin and have the first execution create the required zip file that the second execution can package into a tar.
But I get the error saying the zip file 'is not a file'.
What is wrong with what I am doing?
Is there a more efficient way to do this?
This is what I tried:
My POM:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>create-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>zip.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>create-tar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>tar.xml</descriptor>d
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
zip.xml
<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
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>zip-id</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/resources/vertica_schema/schema_migrations</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
tar.xml
<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/plugins/maven-assembly- plugin/assembly/1.1.2"
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>tar-id</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>/*.zip</source>
<outputDirectory>/lib</outputDirectory>
<destName>schema_migrations.zip</destName>
</file>
</files>
The <source> tag does not support *: it is only possible to select a single file with it. Additionally, the zip file build by the first execution will be generated inside the target folder by default, so you should point to that directory.
By default, the zip file will be named ${project.build.finalName}-zip-id.zip. You can change this name by setting the finalName attribute in the plugin configuration. Note that, by default, the assembly id will be concatenated to the finalName. You can turn this off by switching the appendAssemblyId flag to false.
This is a working configuration:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>create-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>zip.xml</descriptor>
</descriptors>
<finalName>myzip</finalName> <!-- Name of zip file -->
<appendAssemblyId>false</appendAssemblyId> <!-- Do not concatenate "zip-id" to the finalName" -->
</configuration>
</execution>
<execution>
<id>create-tar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>tar.xml</descriptor>d
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
with the following tar.xml:
<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/plugins/maven-assembly- plugin/assembly/1.1.2"
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>tar-id</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>${project.basedir}/target/myzip.zip</source>
<outputDirectory>/lib</outputDirectory>
<destName>schema_migrations.zip</destName>
</file>
</files>
It is a good practice to keep everything that is generated by Maven under the target directory. This is the working folder of Maven and you should not worry about keeping intermediate files in there.
I am trying to figure out how to create a zip file of my Javadocs instead of a jar file using Maven. I am currently creating a jar file using the maven-javadoc-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>attach-javadoc</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
I looked at the goals listed here but found nothing helpful to this end. Is this possible using maven-javadoc-plugin or must some other plugin be used?
You need to use maven-javadoc-plugin in combination with maven-assembly-plugin to package the generated docs in a zip. First, only generate the apidocs using javadoc:javadoc goal, but don't package it. This generates the docs in ${project.build.directory}/apidocs by default.
Then, use maven-assembly-plugin to package these docs as follows:
Add maven assembly plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>docs-assembly</id>
<phase>package</phase>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assemble.xml</descriptor>
</descriptors>
</configuration>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Now, configure maven assembly plugin:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>${project.build.finalName}</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/apidocs</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
I have a maven question.
I have a GWT project which generates a temporary directory gwt-unitCache folder. I'd like to remove it at the end of the build process.
I have got this plugin configuration:
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<configuration>
<filesets>
<fileset>
<directory>src/main</directory>
<includes>
<directory>gwt-unitCache/**</directory>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
<executions>
<execution>
<id>gwt-unitCache</id>
<phase>install</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
This does its job by deleting the automatically generated gwt-unitCache folder under src/main. However, it also removed the target folder which contains the classes and war file.
I don't want the target file to be removed thus I modified the configuration by removing the section:
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<configuration>
<filesets>
<fileset>
<directory>src/main</directory>
<includes>
<directory>gwt-unitCache/**</directory>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
<executions>
<execution>
<id>gwt-unitCache</id>
<phase>install</phase>
</execution>
</executions>
</plugin>
But this time it does not work anymore. The gwt-unitCache is not removed. It looks like the plugin is run at the beginning of the build process rather than at the end.
I am not good at Maven. Can someone help with this? How can I configure it so that:
The gwt-unitCache is removed at the end of the build process.
The target folder is not removed.
I use the command:
maven clean install
to build it.
Many thanks.
Not tested but maven-clean-plugin exposes an excludeDefaultDirectories that could help do the job. Something like:
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<configuration>
<filesets>
<fileset>
<directory>src/main</directory>
<includes>
<directory>gwt-unitCache/**</directory>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
</configuration>
<executions>
<execution>
<id>gwt-unitCache</id>
<phase>install</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
On a side note, I don't see why you need to clear this directory, can't you just ignore it in your SCM?
RC I use this variation to still working as usual on clean phase.
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>gwt-unitCache</id>
<phase>install</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>${project.build.directory}/${project.build.finalName}/c</directory>
<includes>
<directory>gwt-unitCache/**</directory>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
</configuration>
</execution>
</executions>
</plugin>
Since it came up in a comment: You can change the location of the cache directory like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<configuration>
<persistentunitcachedir>${project.build.directory}</persistentunitcachedir>
</configuration>
</plugin>