Attaching main artifact if built by maven-assembly-plugin - java

I have issues building my maven project. It requires building two jars - one of them is built by default maven-jar-plugin which just contains the complied class folders of my Java code and the other, customized one made by maven-assembly-plugin in package phase is an executable jar with all the dependency jars added.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-
plugin:2.2-beta-5:single (executable-jar-generation) on project enhanced-
search: Execution executable-jar-generation of goal
org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single failed: For
artifact {com.fmr.es:enhanced-search:1.0:jar}: An attached artifact must
have a different ID than its corresponding main artifact. -> [Help 1]
I've been following Manually attach main artifact if built by maven-assembly-plugin as reference since the problem is more or less same.
As the answer says, I add the ant-plugin code which overwrites my main jar with the executable one to install on the repository with the package phase.
But before maven even reaches this plugin, the above error crops up. I can't do the vice-versa since there has to be the executable jar created in the first place to overwrite the main one.
Anymore feedback is really appreciated.
The following is the assembly plugin which creates my executable jar along with the assembly.xml
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<id>executable-jar-generation</id>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>executable-enhanced-search-1.0</finalName>
<descriptors>
<descriptor>assembly-jar.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-
plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven
assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-
1.1.2.xsd">
<baseDirectory>/</baseDirectory>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>../../target/classes</directory>
<includes>
<include>**/*</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
The following is the tailored ant task as mentioned in the link I posted above.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy file="../../target/executable-enhanced-search-
1.0.jar" tofile="../../target/enhanced-search-
1.0.jar" overwrite="true" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>

Related

how to create multiple jars with usage specific xml copied to jar - Maven

I am trying to migrate from ANT build to Maven build setup for a very simple codebase setup:
src > Java Classes
conf > META-INF > 3 xml files namely dbConnect-jboss.xml, dbConnect-weblogic.xml, dbConnect-ooc.xml
The goal is to create 3 JARs :
project-jboss.jar, project-weblogic.jar, project-ooc.jar
where each jar will have META-INF/dbConnect.xml, copy of relevant conf/META-INF/dbConnect-xxx.xml.
I tried with [maven-jar-plugin + maven-antrun-plugin ] but the issue is the maven-antrun-plugin does the copy only one time so suppose project-jboss.jar created first then all the rest jars will have same dbConnect.xxx.xml
I need to get a way - how to invoke the copy of dbConnect.xml file via maven-antrun-plugin each time for the respective JAR creation.
screenshot for maven-ant and maven-jar plugin section from pom.xml
Thanks for all those who tried to help me out here, appreciate your helping gesture.
I opted maven-assembly-plugin for the needful.
The following snippet would explain my approach:
<pluginManagement>
<plugins>
<plugin>
<!-- To suppress default JAR creation -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase />
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<finalName>${project.artifactId}</finalName>
<useProjectArtifact>false</useProjectArtifact>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
<executions>
<execution>
<id>create-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
Then I created respective assembly xml files for each relevant JAR expected. For example, one of assembly xmls:
<assembly ....>
<id>jboss</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<!-- Avoid getting relevant dependencies included in JAR -->
<includes>
<include>com.vibrant:streamliner.reposerv</include>
</includes>
<useTransitiveDependencies>false</useTransitiveDependencies>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
<files>
<file>
<source>${project.basedir}/config/META-INF/dbConnect-jboss.xml</source>
<outputDirectory>META-INF</outputDirectory>
<destName>dbConnect.xml</destName>
</file>
</files>
Then, I added profile in pom.xml:
<profile>
<id>create-all-jars</id>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>pom-assembly-jboss.xml</descriptor>
<descriptor>pom-assembly-weblogic.xml</descriptor>
<descriptor>pom-assembly-ooc.xml</descriptor>
</descriptors>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</profile>
And, once we execute following - 3 Jars get created:
mvn -P create-all-jars clean package

Maven dependency plugin adds artifact named directory

I want to copy files from some artifact. but it always adds a directory with the name of that artifact.
the pom of the artifact to copy from:
<groupId>some.group</groupId>
<artifactId>scheduler-common-test-resources</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Scheduler common test resources</name>
<description>A scheduler test resources</description>
<packaging>pom</packaging>
.
.
.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>lib/assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
the assembly file:
<assembly>
<id>json</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>resources/db</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>alterTables.sql</include>
<include>createTables.sql</include>
<include>insertsIntoReminders.sql</include>
</includes>
<excludes>
<exclude>pom.xml</exclude>
</excludes>
</fileSet>
</fileSets>
the item to be copied in the artifact pom:
<artifactItem>
<groupId>some.group</groupId>
<artifactId>scheduler-common-test-resources</artifactId>
<version>1.0.0-SNAPSHOT</version>
<outputDirectory>${project.build.directory}/test-classes/db/</outputDirectory>
<type>tar.gz</type>
<overWrite>false</overWrite>
</artifactItem>
result:
its get copied to test-classes/db/scheduler-common-test-resources-1.0.0-SNAPSHOT/
how can i remove the directory with the artifact name?
The assembly-plugin will by default add a baseDirectory, which will, also by default, be ${project.build.finalName}.
In your case, you just have to indicate the plugin that you don't need that directory by adding:
<includeBaseDirectory>false</includeBaseDirectory>
in the assembly descriptor (assembly.xmlfor you). See assembly descriptor documentation.

Using Maven to create Javadocs as a zip file instead of a jar file

I am trying to figure out how to create a zip file of my Javadocs instead of a jar file using Maven. I am currently creating a jar file using the maven-javadoc-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>attach-javadoc</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
I looked at the goals listed here but found nothing helpful to this end. Is this possible using maven-javadoc-plugin or must some other plugin be used?
You need to use maven-javadoc-plugin in combination with maven-assembly-plugin to package the generated docs in a zip. First, only generate the apidocs using javadoc:javadoc goal, but don't package it. This generates the docs in ${project.build.directory}/apidocs by default.
Then, use maven-assembly-plugin to package these docs as follows:
Add maven assembly plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>docs-assembly</id>
<phase>package</phase>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assemble.xml</descriptor>
</descriptors>
</configuration>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Now, configure maven assembly plugin:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>${project.build.finalName}</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/apidocs</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>

Specify common resources in a multi-module maven project

Is there any way to share resources between modules of a parent project in Maven? For example, I would like to specify one log4j.properties file for all the modules in a multi-module Maven project.
Generally, I use Eclipse IDE to create the parent project by choosing a general project and then convert it to a Maven project by specifying a packaging of pom. This creates a "clean" project structure without src and etc. folders. I wonder where such a shared resource should be put in this case.
EDIT1: I would like to put the common resources in the parent project.
I'd create one additional "base" module (project), packaging "jar", that contains the common resources in src/main/resources. Then I'd make the other modules depend on that project. Now they see the common resources on their classpaths.
Antoher possibility is to use a remote resource bundle. You would be able to configure it in the parent project. In this example I wanted to copy some files just for tests. If you use this you will need to create the bundle in another project.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<configuration>
<resourceBundles>
<resourceBundle>es.sca:myBundle:1.0.0</resourceBundle>
</resourceBundles>
<attachToMain>false</attachToMain>
<attachToTest>true</attachToTest>
<appendedResourcesDirectory>${basedir}/src/test/resources</appendedResourcesDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
Another way, put in your project root pom:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<!-- don't propagate to child poms -->
<!-- this will only execute in root pom -->
<inherited>false</inherited>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<!-- don't add classifier -->
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugins>
And example of assembly.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>resources</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/resources/</directory>
<outputDirectory/>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Assembly plugin will generate artifact and attach it to current reactor, so it will be installed and deployed.
No you can use it as standard dependency event in the same pom.
Important is to trigger assembly (proper phase) before another plugin which will use generated artifact.
Eg. You can have in your root pom, bellow configuration will be propagated to all your module:
<plugin>
<artifactId>some-maven-plugin</artifactId>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>goal</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>your.project.groupid</groupId>
<artifactI>your.project.artifactId</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
You can see this method in project:
https://github.com/s4u/pgp-keys-map resources directory is shared by all module.
Yes, it seems as a possible solution. But I was interested whether it
is possible to specify these resources in the parent project (without
introducing additional module) since the parent project specifies all
the common dependencies and Maven configurations for the child
modules, I think that the parent project is the most suitable place
also for the common resources.
In case of packaging type pom , when goal package specified to manage your shared resources, just add next (check folders) into build section of pom file :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-config-files</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/logconfig</outputDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I think you can just add the resources and/or testResources elements to your pom.
E.g. to access an additional test resource directory add:
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>../global/src/test/resources</directory>
</testResource>
</testResources>
see Maven - Override test resource folder
I managed it to work like this:
I create a project/assembly/test/resources/META-INF/persistence.xml file, and add this to my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-test-persistence-xml-resources</id>
<phase>process-test-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/</outputDirectory>
<resources>
<resource>
<directory>${project.parent.basedir}/assembly/</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Maven Spring jar packaging

We have created a jar(Spring project) which we are including using Maven in another Spring project (war)
we can use this no problem if we include the required dependancies.
If I wanted to ensure the jar contained all it's depenadancies and used them what is the best way to go about this ?
To avoid having to include the dependancies I have tried in using the maven assembly plugin
which definately includes the files - but these appear to be ignored as they are still required as dependancies in the consuming project - any suggestions why?
POM detail for assembly plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</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>
You can use the maven winstone plugin to create a standalone executable jar with all dependencies included of the war, the jar etc....
Adding
<plugins>
<plugin>
<groupId>net.sf.alchim</groupId>
<artifactId>winstone-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<goals>
<goal>embed</goal>
</goals>
<phase>install</phase>
</execution>
</executions>
<configuration>
<filename>${project.parent.build.finalName}.jar</filename>
</configuration>
</plugin>
...
</plugins>
will bundle all modules of your app, wars and jars, and any needed dependencies in a executable jar which includes a servlet engine.
This works really nice to give a single deployable bundle to your Ops team for deploying.
I would advise against creating a jar which contain all dependencies to be used by the war however. You are likely to end up with multiple copies of slightly different versions of the same classes in the class path. Certain libraries are a bit euhhmmm, ..., temperamentfull about this (hibernate and log4j spring to mind).
Maven does a decent job of figuring out which dependencies to take, and if things break mvn dependency:tree make things a lot clearer. You lose this when you create an uber-jar.
If there are good reasons to do this, I would recommend an environment where you can tightly control the classpaths like a full-blown J2EE server or using an OSGi container. However, be careful what you wish for : these are not kittens to be handled without (iron) gloves.
I use the following assembly configuration to create a jar with all runtime dependencies included:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>runnable</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>

Categories

Resources