我们的项目依赖于其他几个公司的项目罐和第三方罐子。在构建期间,我们使用${project.build.directory}/third-party-jars/
目标将它们复制到两个单独的文件夹${project.build.directory}/mycompany-jars/
和maven-dependency-plugin:copy-dependency
,如下所示:
<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>
请注意,我使用<excludeGroupIds>com.mycompany</excludeGroupIds>
和<includeGroupIds>
对象来区分第三方和公司的jar,同时将它们复制到不同的文件夹。
接下来,在将这些jar添加到jar内的manifest文件中的classpath属性时,我想在这些类路径中附加适当的前缀目录。例如,我想使用../thirdparty-jars/
为第三方广告加前缀,而使用../mycompany-jars/
为mycompany广告加前缀。
为此,我使用maven-antrun-plugin
,类似于此(这涉及获取所有依赖项的绝对路径字符串,然后运行正则表达式修剪前缀到target
,然后附加到mycompany-jar
或thirdparty-jar
根据要求):
<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>
我们需要为许多具有共同父母pom的项目执行此操作。因此,为了避免将此复制到所有子poms,我将此代码移动到父pom。但是,不知何故,我在执行父pom中包含的maven-antrun-plugin
jar
任务时遇到了问题。每次从{child}执行maven-jar-plugin
jar
任务。我已在此question中讨论过这个问题。
Q1。我目前的问题是,是否有任何标准的,更首选的方法,可能是为了避免maven-antrun-plugin
而是使用maven-jar-plugin
。
Q2。我遇到了<classpathPrefix>
,如第34节中所述;更改类路径:定义类路径目录前缀&#34; here。但问题是如何运行我在maven-antrun-plugin
中使用<classpathPrefix>
实施的自定义逻辑。 maven-jar-plugin
是否提供类似这样的功能?