packing rxtx library in a jar with maven assembly plugin - maven

i need to create a jar file with dependencies and i need to add rxtx library as a dependency. but when i create the jar i cannot see rxtx in the jar file. please post me the correct way to do this.
this is my relevant part of the pom file
....
<dependency>
<groupId>org.rxtx</groupId>
<artifactId>rxtxcomm</artifactId>
<version>2.0-7pre1</version>
<scope>run</scope>
</dependency>
....
....
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>uom.elect.smeter.Output</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- this is used for inheritance merges -->
<phase>package</phase>
<!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
....

<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>process-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.company.project</groupId>
<artifactId>RXTXcommJar</artifactId>
<version>1.0.0</version>
<type>jar</type>
<outputDirectory>
${project.build.outputDirectory}
</outputDirectory>
<destFileName>RXTXcomm.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.company.project</groupId>
<artifactId>rxtxParalleldll</artifactId>
<version>1.0.0</version>
<type>dll</type>
<outputDirectory>
${project.build.outputDirectory}/apps/plugin/
</outputDirectory>
<destFileName>rxtxParallel.dll</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.company.project</groupId>
<artifactId>rxtxSerialdll</artifactId>
<version>1.0.0</version>
<type>dll</type>
<outputDirectory>
${project.build.outputDirectory}/apps/plugin/
</outputDirectory>
<destFileName>rxtxSerial.dll</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

Related

Super pom and plugins

According to this book I am reading, in my super pom I must find the following code :
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
While in my super pom I find no goals or anything specified, like so :
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
</plugin>
The only plugin which was specified is the dependency plugin. The goal was set to copy:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>/home/hp-pc/test_maven/project/target/endorsed</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
Can you explain why I find this difference and what's "copy" defined in the goal ?

In maven-dependency-plugin how to unpack only if I pass him parameter?

Does Anyone knows how I can unpack an artifact by terms?
Meaning giving him like a Boolean parameter that will determine whether or not to unpack that certain artifact.
I tried to use the skip flag but it didn't work.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>get-rpm</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.XX.XXX</groupId>
<artifactId>XXX-ONPREM</artifactId>
<version>${BUILD_NUMBER}</version>
<type>rpm</type>
<classifier>rpm</classifier>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
<destFileName>${PACKAGE_NAME}</destFileName>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</execution>
<execution>
<id>get-third-parties</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.XX.XXX</groupId>
<artifactId>thirdparties-sources</artifactId>
<version>${third-parties.version}</version>
<type>zip</type>
<skip>true</skip>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/3rd-parties</outputDirectory>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</execution>
So I figured it out,
You simply have to put the skip flag in the right place,
I've put it right after the configuration.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>get-rpm</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.XX.XXX</groupId>
<artifactId>AGM-ONPREM</artifactId>
<version>${BUILD_NUMBER}</version>
<type>rpm</type>
<classifier>rpm</classifier>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
<destFileName>${PACKAGE_NAME}</destFileName>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</execution>
<execution>
<id>get-third-parties</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<skip>${SKIP_3rd}</skip>
<artifactItems>
<artifactItem>
<groupId>com.XX.XXX</groupId>
<artifactId>thirdparties-sources</artifactId>
<version>${third-parties.version}</version>
<type>zip</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/3rd-parties</outputDirectory>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</execution>

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>

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