How to remove a a specific directory from the Maven's target directory at the end of the build? - maven

I have a task to unpack all the jars mentioned in the pom.xml and then jar the unpacked content into one single jar. I am able to do this using the unpack-dependency goal of the dependency plugin and the jar plugin.
However, after i generate the new jar, I want to delete the folder that was created after unpacking. I am using the following code snippet in my pom.xml(Please read the comments above each plugin).
<build>
<plugins>
<!-- This part of the code is used to unpack all the dependencies mentioned in my pom.xml -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includes>**/*.class</includes>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<!-- This part of the code is used for creating a jar with all the contents of the "alternateLocation" directory above -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${project.build.directory}/alternateLocation</classesDirectory>
<outputDirectory>${project.build.directory}</outputDirectory>
<finalName>abc</finalName>
</configuration>
</execution>
</executions>
</plugin>
<!-- I am using this part of the code to delete the "alternateLocation" after everything is done, but it deletes the target directory instead -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>install</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>${project.build.directory}/alternateLocation</directory>
</fileset>
</filesets>
<excludeDefaultDirectories>${project.build.directory}</excludeDefaultDirectories>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The clean plugin is basically deleting the target directory by default.
So how can I delete the "alternateLocation" folder from the target directory at the end of the build. (I guess you can do it using the maven-antrun-plugin but i don't want to use this plugin.).

You can solved this by using the maven-assembly-plugin via the predefined descriptor jar-with-dependencies which can be done by the following:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
</project>
Than you don't need supplemental configuration.

Related

Spring Boot jar - META-INF inside BOOT-INF folder

Is there any way to have META-INF folder inside BOOT-INF in spring boot packaging jar?
This might be a trivial question, but not sure how to do it. Below is the way i am packaging right now which created three folders BOOT_INF, META_INF, ORG. The reason of asking this is because runnable jar is not able to locate a folder which is inside META-INF.
<build>
<finalName>XYZ</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>XYZ</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<excludes>
<exclude>XYZ</exclude>
<exclude>XYZ</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>XYZ</mainClass>
<addResources>true</addResources>
<wait>1000</wait>
<maxAttempts>250</maxAttempts>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>XYZ</version>
<configuration>
<tarLongFileMode>XYZ</tarLongFileMode>
<tarLongFileMode>XYZ</tarLongFileMode>
<descriptors>
<descriptor>XYZ</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>XYZ</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any help which plugin i can use in order to do that?
Similar issue resolved in my case after adding META-INF folder under resources.

How to skip version in jar generated by maven-jar-plugin and clasifier

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>client</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>client</classifier>
<includes>
<include>**/com.service/**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
I want the version to be appended to end of the jar.Right now it is building jar like ship-service-0.1-client.jar,I want it like ship-service-client-0.1.jar and i need to add it as dependency in another project.
You can use
<build>
<finalName>FinalNameOfYourProyect</finalName>
</build>
check this related question

Maven rename a file after everything else finishes

I have a project which I need to rename the final output file generated by the Maven Assembly Plugin after everything else finishes (in the compiling/building/assembly process).
The Maven Assembly Plugin is generating a final .zip file, based on the project's name, and I need to rename this completely to final-version.oxt. I'm trying to use the maven-antrun-plugin to rename it, as pointed by other similar questions here, but no luck (I've never used Maven or Ant before, so maybe I'm missing something).
This is the <build> section of the project's pom.xml. The rename part seems to be completely ignored, since no file is generated in my home folder.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
<descriptors>
<descriptor>src/main/assembly/ooo-jar.xml</descriptor>
<descriptor>src/main/assembly/ooo.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>deploy</phase>
<configuration>
<tasks>
<copy file="${project.build.directory}/target/libreofficeplugin-ooo.zip"
tofile="/home/brunofinger/final-version.oxt" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
A few modifications made it work, probably the phase was wrong, but using <phase>install</phase> seems to make it work:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project.build.directory}/libreofficeplugin-ooo.zip" tofile="${project.build.directory}/final-version.oxt" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
This question is nearly an year old, but in case anyone else is facing similar issue, here is an alternate solution.
If you are looking for a more mavenish way of doing it,you can use
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>rename-file</id>
<phase>install</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<sourceFile>${project.build.outputDirectory}/libreofficeplugin-ooo.zip</sourceFile>
<destinationFile>${project.build.outputDirectory}/final-version.oxt</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
and in case you want to copy instead of rename, use the copy goal instead of the rename goal.
You don't need to use antrun to rename the output file. Just use the tag finalName of the assembly plugin to rename the output file.
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
<descriptors>
<descriptor>src/main/assembly/ooo-jar.xml</descriptor>
<descriptor>src/main/assembly/ooo.xml</descriptor>
</descriptors>
<finalName>final-version.oxt</finalName>
</configuration>
</execution>
</executions>
</plugin>

How can I combine jars using Maven?

The following snippet creates 2 JARS:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>build-dependency</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>dependency</classifier>
<classesDirectory>${project.build.directory}\dependency</classesDirectory>
<includes>
<include>${dependancyInclude}</include>
</includes>
</configuration>
</execution>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>module</classifier>
<classesDirectory>${project.build.directory}\classes</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
I would like to combine these two JARs into one using the assembly plugin, currently I have the following:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<executions>
<!-- Combine all the JARs in the /target folder into one JAR -->
<execution>
<id>make-assembly</id>
<phase>compile</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<attach>true</attach>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}-${project.version}</finalName>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
Currently only one of the two JARS in included in the final JAR that is created by the assembly plugin.
If I understand you correctly, you actually want to merge the jar files into one big jar file. This can be achieved using the maven-shade-plugin (instead of the maven-assembly-plugin).
For example:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<configuration>
<!-- Put your configuration here, if you need any extra settings.
(You should be okay with the defaults). -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>

unpack dependency and repack classes using maven?

I am trying to unpack a maven artifact A and repack it into a new jar file in the maven project B.
Unpacking class files from artifact A into:
<my.classes.folder>${project.build.directory}/staging</my.classes.folder>
works fine using this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.test</groupId>
<artifactId>mvn-sample</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${my.classes.folder}</outputDirectory>
<includes>**/*.class,**/*.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
In the same pom I now want to generate an additional jar containing the classes just unpacked:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesdirectory>${my.classes.folder}</classesdirectory>
<classifier>sample</classifier>
</configuration>
</execution>
</executions>
</plugin>
A new jar is created but it does not contain the classes from the:
${my.classes.folder}
its simply a copy of the default project jar. Any ideas?
I have tried to follow this guide:
http://jkrishnaraotech.blogspot.dk/2011/06/unpack-remove-some-classes-and-repack.html
but its not working.
I would suggest you to use the maven-shade-plugin instead.
That will make the unpack-repack in the same invocation.
You could do something like this for example:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.test:mvn-sample:1.0.0-SNAPSHOT</artifact>
<includes>
<include>**/*.class</include>
<include>**/*.xml</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
In the sample you have <classesdirectory>, the docs have the element as <classesDirectory>. Case sensitivity matters, I think.

Resources