How to rename a file while unpacking an artifact with maven-dependency-plugin? - maven

I'm using a maven-dependency-plugin to unpack a jar that contains several .sh scripts and that works as expected.
So I'm able to copy the files into the <outputDirectory>${project.build.dir}/main-scripts</outputDirectory>
But now I want to rename only one of them from run.sh to run-main.sh while unpacking. How could it be achieved with the dependency plugin?
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.main.libs</groupId>
<artifactId>main-scripts</artifactId>
<version>LATEST</version>
<type>jar</type>
<includes>*.sh</includes>
<outputDirectory>${project.build.dir}/main-scripts</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

Related

how does copy-resources work

I tried using maven-resources-plugin with goal copy resources.It copied all the resources to the output directory.I needed only a single jar.How to make it possible?
My requirement was to add a maven dependency to resources folder so atht I can add it to my Kie-Container and execute the rules residing inside the jar.I achieved it using maven-dependency-plugin with goal "copy".The mistake I did was not specifying the artifactItem.Afte I used artifactItem and included my jar description it worked.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy</id>
<phase>prepare-package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>SampleDroolsWBTest</groupId>
<artifactId>DroolsAPI</artifactId>
<version>2.0</version>
<overWrite>false</overWrite>
<outputDirectory>${basedir}/src</outputDirectory>
<includes>pom.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

Unpack jar contents after creating it in maven

I have this plugin in my pom.xml that creates a jar file and place it somewhere :
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<outputDirectory>${basedir}/../../web/src/main/docroot/WEB-INF/lib</outputDirectory>
</configuration>
</plugin>
I want to extract this jar into some directory after creating it. How can I do that ?
I found this solution and it works for me: :)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>ir.nsdp.satra</groupId>
<artifactId>Shapeloader</artifactId>
<version>1.0</version>
<type>jar</type>
<excludes>META-INF/**</excludes>
<outputDirectory>${basedir}/../../../web/src/main/docroot/WEB-INF/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

Maven extract Dependency without creating dependency directory

The configuration below works but I end up with an extra directory that I don't want. So I have target/webapp/dep-A/<depedency contents>, what do i need to change to get it to be target/webapp/<depedency contents>?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>myGroup</groupId>
<artifactId>dep-A</artifactId>
<version>1.0-SNAPSHOT</version>
<type>zip</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/webapp</outputDirectory>
</artifactItem>
</artifactItems>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
Unfortunately the maven-dependency-plugin unpack goal has not a postprocess optional parameter letting you, for example, chmod-ing or moving files. So I don't think exists an elegant way to do what you want only using the maven-dependency-plugin.
In addition to the maven-dependency-plugin you might use the maven-antrun-plugin, binding it to a subsequent phase which the maven-dependency-plugin is binded to, to copy/move/delete your files and directories.

Build maven jar from classes no java source

I want to build a maven jar artifact from classes. I don't have source files. These classes are originally in another artifact installed locally. I use maven-dependency-plugin to unpack the classes and put them in the target folder for this project/module.
It creates the jar.. but doesn't include the classes I just unpacked. Here's my pom:
<build>
...
<!-- unpack myjar1.jar and myjar2.jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.company</groupId>
<artifactId>myjar1</artifactId>
<version>1.0</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>target/final</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>com.company</groupId>
<artifactId>myjar2</artifactId>
<version>1.0</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>target/final</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>/path/to/target/final/folder</classesDirectory>
<includes>
<include>**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
How can I include these classes into my final.jar?
I think the best solution is the maven-shade-plugin: create a pom.xml, add those 2 libraries as dependencies and configure the maven-shade-plugin.
Run mvn package and you have your merged project.
What Robert wrote above might be a workable solution too.. but I figured a different way out. I simply removed <includes> inside the maven-jar-plugin and it worked. I ran the build in eclipse by creating a build configuration and chose "debug" option. It spit out a lot of info about "configuration" which is otherwise not displayed.
Thanks!
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>path/to/final/folder</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
This worked!
Another approach is to set ouputDirectory to regular target/classes directory.
target/classes
So that unpacked classes plus your project classes will be avaialble in target/classes which can be bundled in to .jar using regular maven-jar-plugin by specifing **
Complete pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>a.b.c</groupId>
<artifactId>aaa</artifactId>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>target/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<includes>
<include>**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

How can I untar an artifact in maven?

How do I untar an artifact to use to compile my source?
Do I need to copy the tar file before untarring it?
I have something like below...
<build>
<plugins>
<plugin>
<artifactId>abcId</artifactId>
<version>1</version>
<executions>
<execution>
<id>abc untar</id>
<phase>process-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>tar</executable>
<workingDirectory>???</workingDirectory>
<arguments>
<argument>xvf abc.tar</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
You can untar an artifact by using dependency:unpack goal of maven dependency plugin. Here is a modified version of the example.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>unpack</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>artifact-groupId</groupId>
<artifactId>the-artifact</artifactId>
<version>a.b</version>
<type>tar</type>
<outputDirectory>${project.build.directory}/artifactLocation</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

Resources