How to add resources of an external jar into my maven build jar? - maven

I have a module , while building this module i want to include some resources from an external jar into my module jar.
I tried to use maven-shade-plugin but I am not able to achieve it it.
Any help is appreciated.

Unpack your external resource into project target, after this unpacked resources will be included in generated artifact.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
<type>jar</type>
<includes>*.xsb, *.properties</includes>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
For non-maven file use method from this answer https://stackoverflow.com/a/3264160/516167

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>

Download war file from nexus with maven

If I have the artifactId, the groupId and the version how I can configure the pom.xml so maven will download test.war file from the nexus repository.
I assume that this should happened in the
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
.....
</plugin>
In the execution block.
I already tried this but no luck:
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<artifactId>mytest</artifactId>
<groupId>com.mytest</groupId>
<version>${version}</version>
<type>war</type>
<destFileName>mytest-${version}.war</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
Are you certain that ${version} is being set properly for you? If you need the version of the current project, you should probably be using <version>${project.version}</version> and possibly setting up <dependencyManagement/> to handle that for you.

Gradle equivalent for mavens maven-dependency-plugin

I'm moving from Maven to Gradle,
In my maven code i used the "maven-dependency-plugin"
and i could not find an easy translation for the following:
so my question is how can i get my dependencies into a specific structure?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy</id>
<phase>prepare-package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.group1</groupId>
<artifactId>artifact1</artifactId>
<version>1</version>
<type>swf</type>
<outputDirectory>/flex/output1</outputDirectory>
<destFileName>artifact1.swf</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.group2</groupId>
<artifactId>artifact2</artifactId>
<version>2</version>
<type>swf</type>
<outputDirectory>/flex/output2</outputDirectory>
<destFileName>artifact2.swf</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.group3</groupId>
<artifactId>artifact3</artifactId>
<version>3</version>
<type>swf</type>
<outputDirectory>/flex/output3</outputDirectory>
<destFileName>artifact3.swf</destFileName>
</artifactItem>
</artifactItems>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
so finding no equivalent in Gradle what i ended up doing is holding a map of artifact id to the new name (outputDirectory + destFileName) and after resolving the dependencies into a new special configuration which i called "Flex" and then renaming every dependencies from "flex" using the mapping.
For those looking for Maven-like dependency management features in Gradle, check out the Dependency Management Plugin. It provides familiar dependencyManagement blocks and BOM support.
Note that you don't need to use Spring in your project to use the 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.

Obfuscate dependencies to single jar with Proguard

I have modular maven application and "module1" depends on "module2" and "module2" depends on "module3" etc.. In module1 I have something like this:
<profile>
<id>obfuscate</id>
<build>
<plugins>
<plugin>
<groupId>com.pyx4me</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<options>
<option>-allowaccessmodification</option>
<option>-keep public class com.test.Main { *; }</option>
</options>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
</libs>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard</artifactId>
<version>${proguard.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
This creates successfully obfuscated "module1". I want to create single jar with all obfuscated dependencies (obfuscated module1 and module2 etc.). Is it possible?
Maven Shade Plugin
This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.
Check it out here.
Try the <assembly> option of the maven-proguard-plugin. Make references to each of the dependencies you wanted bundled into the obfuscated jar. See examples.
If you want package dependency jars into your uber jar, use shade plugin! here's an example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

Resources