我正在使用Maven构建一个包含20多个工件和~30个第三方工件的EAR项目。 EAR结构看起来像这样
EAR
+-project-artifact-1
+-project-artifact-2
...
+-project-artifact-n
+-lib/
| +-3rd-party-lib-1
| +-3rd-party-lib-2
| ...
| \-3rd-party-lib-n
\-META-INF/
包装工作正常。
我的问题是为MANIFEST.MF定制了Class-Path
我想使用Mavens功能使用为我创建MANIFEST.MF
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
我知道我可以使用classpathPrefix
来设置前缀,但是这个前缀将被添加到所有依赖项中,无论它们的位置如何。
由于技术原因,我无法将所有JAR类型的工件放在lib文件夹中。
唯一的解决方法是将项目工件依赖项设置为scope provided
,并使用manifestEntries
手动将它们添加到类路径中。
我不喜欢这种解决方法,因为很难维护并有效地杀死我的依赖关系的传递性(我想保留它)。
我发现了这个example for a custom class-path,但我不知道如何将它用于我的目的。我的想法是使用属性来操作类路径,但似乎你只能使用工件的某些属性作为变量。
是否有其他方法来构建CP(除了使用手动维护的MF或手动维护poms中的CP条目)?
这是我的父pom看起来像(缩短):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.app</groupId>
<artifactId>build-parent</artifactId>
<version>0.1</version>
<packaging>pom</packaging>
<name>build parent</name>
<description>
</description>
<repositories>
<repository>
<id>myapp-internal</id>
<url>file:///${user.home}/.m2/repository</url>
<releases>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>
</repositories>
<properties>
<!-- variables -->
<!-- myapp -->
<myapp.version>0.1</myapp.version>
<myapp.encoding>Cp1252</myapp.encoding>
<!-- config variables -->
<directory.target>../myapp_build/target</directory.target>
<version.ejb>3.1</version.ejb>
<!-- 3rd party lib versions -->
<version.slf4j>1.7.10</version.slf4j>
<!-- maven plugin versions -->
<version.plugin.buildhelper>1.3</version.plugin.buildhelper>
<version.plugin.maven.assambley>2.2.1</version.plugin.maven.assambley>
<version.plugin.maven.compiler>3.1</version.plugin.maven.compiler>
<version.plugin.maven.ear>2.9.1</version.plugin.maven.ear>
<version.plugin.maven.ejb>2.3</version.plugin.maven.ejb>
<version.plugin.maven.jar>2.4</version.plugin.maven.jar>
<version.plugin.maven.war>2.4</version.plugin.maven.war>
<!-- settings -->
<project.build.sourceEncoding>${myapp.encoding}</project.build.sourceEncoding>
<project.reporting.outputEncoding>${myapp.encoding}</project.reporting.outputEncoding>
</properties>
<!-- define default dependency details -->
<dependencyManagement>
<dependencies>
<!-- myapp dependencies -->
<!-- com.my.app -->
<dependency>
<groupId>com.my.app</groupId>
<artifactId>artifact1</artifactId>
<version>${myapp.version}</version>
</dependency>
<dependency>
<groupId>com.my.app</groupId>
<artifactId>artifact2</artifactId>
<version>${myapp.version}</version>
</dependency>
<dependency>
<groupId>com.my.app</groupId>
<artifactId>myapp_ejb</artifactId>
<version>${myapp.version}</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>com.my.app</groupId>
<artifactId>myapp_web</artifactId>
<version>${myapp.version}</version>
<type>war</type>
</dependency>
<!-- 3rd party dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${version.slf4j}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${version.slf4j}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!-- use Eclipse style structure -->
<sourceDirectory>src</sourceDirectory>
<outputDirectory>bin</outputDirectory>
<!-- target -->
<directory>${directory.target}</directory>
<!-- define default plugin settings -->
<pluginManagement>
<plugins>
<!-- assambley -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${version.plugin.maven.assambley}</version>
</plugin>
<!-- build helper -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${version.plugin.buildhelper}</version>
</plugin>
<!-- compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.plugin.maven.compiler}</version>
<configuration>
<source>${version.java}</source>
<target>${version.java}</target>
</configuration>
</plugin>
<!-- ear -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>${version.plugin.maven.ear}</version>
</plugin>
<!-- ejb -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>${version.plugin.maven.ejb}</version>
<configuration>
<ejbVersion>${version.ejb}</ejbVersion>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<!-- jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${version.plugin.maven.jar}</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</archive>
</configuration>
</plugin>
<!-- war -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${version.plugin.maven.war}</version>
<configuration>
<!-- use provided web.xml -->
<webXml>WebContent/WEB-INF/web.xml</webXml>
<!-- ressources location -->
<warSourceDirectory>WebContent</warSourceDirectory>
<!-- build a skinny WAR -->
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
这是我(缩短)的EAR:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.my.app</groupId>
<artifactId>build-parent</artifactId>
<version>0.1</version>
<relativePath>../myapp_build</relativePath>
</parent>
<groupId>com.my.app</groupId>
<artifactId>myapp_application_EAR</artifactId>
<version>${myapp.version}</version>
<description />
<packaging>ear</packaging>
<dependencies>
<!-- myapp dependencies -->
<dependency>
<groupId>com.my.app</groupId>
<artifactId>myapp_web</artifactId>
<type>war</type>
</dependency>
<dependency>
<groupId>com.my.app</groupId>
<artifactId>myapp_ejb</artifactId>
<type>ejb</type>
</dependency>
<dependency>
<groupId>com.my.app</groupId>
<artifactId>artifact1</artifactId>
</dependency>
<dependency>
<groupId>com.my.app</groupId>
<artifactId>artifact2</artifactId>
</dependency>
<!-- 3rd party dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<generateApplicationXml>true</generateApplicationXml>
<!-- skinny WAR configuration see WAR project POM -->
<skinnyWars>true</skinnyWars>
<modules>
<webModule>
<groupId>com.my.app</groupId>
<artifactId>myapp_web</artifactId>
<contextRoot>myapp_web</contextRoot>
</webModule>
<ejbModule>
<groupId>com.my.app</groupId>
<artifactId>myapp_ejb</artifactId>
</ejbModule>
<!-- 3rd party libs go to lib/ -->
<jarModule>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<bundleDir>lib/</bundleDir>
<includeInApplicationXml>false</includeInApplicationXml>
</jarModule>
<jarModule>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<bundleDir>lib/</bundleDir>
<includeInApplicationXml>false</includeInApplicationXml>
</jarModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>