Our project have dependency on several other company project jars and third party jars. During build, we are copying them to two separate folders ${project.build.directory}/third-party-jars/
and ${project.build.directory}/mycompany-jars/
using maven-dependency-plugin:copy-dependency
goal as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>build-thirdparty-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<prefix>${jar.folder.thirdparty}</prefix>
<outputDirectory>${project.build.directory}/thirdparty-jars/</outputDirectory>
<excludeGroupIds>com.mycompany</excludeGroupIds>
</configuration>
</execution>
<execution>
<id>build-mycompany-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<prefix>${jar.folder.mycompany}</prefix>
<outputDirectory>${project.build.directory}/mycompany-jars/</outputDirectory>
<includeGroupIds>com.mycompany</includeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
Note that I am using <excludeGroupIds>com.mycompany</excludeGroupIds>
and <includeGroupIds>
counterpart for differentiating between third party and company jars while copying them to different folders.
Next while adding these jars to the classpath property in manifest file inside jar, I want to append appropriate prefix directory to those classpaths. For example, I want to prefix third party jars with ../thirdparty-jars/
, while prefix mycompany jars with ../mycompany-jars/
.
For this I am using maven-antrun-plugin
, something like this (this involves getting absolute path string of all dependencies and then running regex to trim prefix till target
and then appending to it mycompany-jar
or thirdparty-jar
as required):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>concat-build-classpath</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<path id="master-classpath">
<fileset dir="${project.build.directory}/mycompany-jars/">
<include name="*.jar" />
</fileset>
<fileset dir="${project.build.directory}/thirdparty-jars/">
<include name="*.jar" />
</fileset>
</path>
<property name="absolute-path" value="${toString:master-classpath}" />
<loadresource property="relative-path">
<propertyresource name="absolute-path" />
<filterchain>
<tokenfilter>
<filetokenizer />
<replaceregex pattern="(^.*?target\\|(?&lt;=;).*?target\\)" replace="" flags="g" />
<replaceregex pattern="(\\)" replace="/" flags="g" />
<replaceregex pattern="(thirdparty-jars)" replace="../thirdparty-jars" flags="g" />
</tokenfilter>
</filterchain>
</loadresource>
<jar destfile="${project.build.directory}/${project.build.finalName}.jar" basedir="src/main">
<manifest>
<attribute name="Class-Path" value="${relative-path}" />
<attribute name="Main-Class" value="com.mycompany.Application" />
<attribute name="Build-Jdk" value="${java.version}" />
<attribute name="Built-By" value="${user.name}" />
</manifest>
</jar>
</target>
</configuration>
</execution>
</executions>
</plugin>
We need to do this for many projects which have common parent pom. So, to avoid replicating this to all child poms, I moved this code to parent pom. But, somehow I am facing issue in executing maven-antrun-plugin
jar
task included in parent pom. Each time maven-jar-plugin
jar
task is getting executed from child. This I have discussed in this question.
Q1. My current question is is there any standard, more preferred approach to do this, may be to avoid maven-antrun-plugin
and use maven-jar-plugin
instead.
Q2. I have came across <classpathPrefix>
as discussed under section "Altering The Classpath: Defining a Classpath Directory Prefix" here. But the issue is how can I run custom logic that I have implemented in maven-antrun-plugin
with <classpathPrefix>
. Does maven-jar-plugin
provides capability something like this?