Maven zip artifact copy, unzip and rename - java

I have a maven module which is packaged as a zip with naming my-artifact-1.0-SNAPSHOT.zip using maven-shade-plugin.
Once the artifact has been installed into local maven repo, I need:
Copy zip to configured local folder.
Unzip archive.
Rename unzipped folder from my-artifact-1.0-SNAPSHOT to my-artifact.
This process should be cross-platform working on Windows, Linux, MacOS machines.
I read that it could be accomplished by using:
maven-dependency-plugin plus another plugin
maven-groovy-plugin
What would the best way to implement such a flow? Any examples are very appreciated.

No need for antrun in my opinion, the maven-dependency-plugin should do the trick:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>validate</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[your.group.id]</groupId>
<artifactId>my-artifact-1.0-SNAPSHOT</artifactId>
<version>[your.version]</version>
<type>zip</type>
<outputDirectory>${project.basedir}/my-artifact/</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Regards,
Tom

You have a maven plugin that can execute Ant-Tasks. This tasks let you execute a lot of works. For example, to unzip a file:
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>generate-resources</phase>
<configuration>
<tasks>
<unzip src="path/to/zip/file.zip" dest="path/to/unzip" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
You can check all availables task in the apache ant page (see references).
Reference:
https://ant.apache.org/manual/Tasks

Related

How can I use Maven to generate a JAR file from the contents of a directory I specify?

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>

maven download plugin to add downloaded resource to jar

I am using download-maven-plugin to download some of resources to be used in my project. The download is successful and I can use the downloaded file.
However, what I want is to include the downloaded file in the JAR.
Note : The resource file will be included in the JAR when it was pre-downloaded before running the build, but if it is not present (e.g. deleted or I want to update it perhaps) the resource file is not included in the JAR.
Basically what I want is to include the recently downloaded file (from mvn clean install) in the output JAR.
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://downloadurl</url>
<outputFileName>error.json</outputFileName>
<outputDirectory>${project.resources.dir}/commons</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Same thing happens when I use maven-ant-run plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>download-files</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<get src="https://downloadurl"
dest="${project.resources.dir}/commons/data.json"
verbose="true"
usetimestamp="true"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
Just download it to ${basedir}/target/classes before packaging phase. You don't need it to be in sources in order to include it to JAR.
Solved!
I changed the phase to process-resources..
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>download-files</id>
<phase>process-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<get src="https://downloadUrl"
dest="${project.resources.dir}/commons/error.json"
verbose="true"
usetimestamp="true"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>

How to copy project target folder to destination location using maven-dependency-plugin

I am migrating my application from JBoss 6 AS to Wildfly 8 AS. As a part of hard deploy, jboss-maven-plugin was employed.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<executions>
<execution>
<id>deploy_ear</id>
<phase>pre-integration-test</phase>
<goals>
<goal>hard-deploy</goal>
</goals>
<configuration>
<deploySubDir>./</deploySubDir>
<fileName>target/${project.build.finalName.full}</fileName>
<unpack>true</unpack>
</configuration>
</execution>
</executions>
</plugin>
In wilfdly 8, I am trying to unpack ear file in standalone/deployments folder and as a part of it, I tried maven-dependency-plugin for copying ear file but it is not working.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>pre-integration-test</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
???
<outputDirectory>${jboss.home}/${jboss.configuration.name}/deployments/test.ear</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
I have the code related to building ear and unpacking the built ear file in the same pom.xml. Can anyone please help me with the configuration as the below tags are pointing to repository configured and hence, resulting in build failure.
<artifactItems>
<artifactItem>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</artifactItem>
How can I point to project build target directory and copy unpacked ear file content in the destination folder.

How to create JavaDoc documentation by using command mvn install?

I have a question concerning the maven javadoc plugin? I have configured that plugin with this values:
<build>
....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<configuration>
<noqualifier>all</noqualifier>
<reportOutputDirectory>${basedir}/MyDoc/javadoc</reportOutputDirectory>
<destDir>javadoc</destDir>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>javadoc</goal>
</goals>
</execution>
</executions>
</plugin>
...
</build>
Is there a way to create some kind of documentation, if I use the command mvn clean install? I donĀ“t want to create a Jar File with my JavaDoc documentation, I need a way to create the JavaDoc and put the created source file directly in my maven project.
Thanks !
Greetz
Marwief
To execute plugin during certain phase, add <phase> to <execution>. Plugin should be fired:
<executions>
<execution>
<id>attach-javadocs</id>
<phase>install</phase> <------ HERE
<goals>
<goal>javadoc</goal>
</goals>
</execution>
</executions>
More on maven lifecycle here

Deploy additional jar file with Maven?

I have an artifact which is being built and deployed in a particular way (not as a jar file). In the course of deployment, a war file is built.
How can I configure the pom so that the artifact is also deployed as a jar file, to a different location?
Maven deploy means deploy artifact to a Maven Repository, not an application server.
Configure additional JAR artifact like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Attach this new artifact to your project:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
<!-- <file>${project.build.directory}/${project.build.finalName}.jar</file> - if finalName is defined -->
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
This blog post and its comments have the answer.
These three plugin configurations will allow you to build/install/deploy a jar version alongside the war.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>
${project.build.directory}/${project.artifactId}-${project.version}.jar
</file>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<generatePom>true</generatePom>
<url>${project.distributionManagement.repository.url}</url>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
</configuration>
</execution>
</executions>
</plugin>
The "maven way" is to split out src/main/java into a separate module, and have the war file depend on that.
If you're absolutely resistant to that approach, you may be able to use a profile to alter the contents of the packaging element. I'm not sure if that's possible though.
Separating them is the right way to go. Forcing maven to produce a war and a jar in the same module is possible but will cause you problems down the road.
You should add the corresponding dependency of the artifact in the dependencies of the pom file.
Ex:
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-api</artifactId>
<version>1.2.2</version>
<scope>compile</scope>
</dependency>
One way to solve this is to have the module build a jar and then use the assembly plugin to build a war file with the jar in WEB-INF/lib of that war. I would strongly recommend against this. You'd be better off having a jar project and a war project with a parent project building both modules.

Categories

Resources