作为从 Java 1.7 到 1.8 的构建迁移的一部分,我试图干净利落地维护 maven 工件的分离(Web 服务器上的当前 JRE 仍在运行 1.7 - 拥有 Java 8 将是一件坏事编译的程序在错误的服务器上)。我正在尝试使用作为配置文件的一部分添加的分类器来清楚地区分构建的工件。
有一个罐子 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>parentpom</artifactId>
<groupId>com.shagie.mvn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>the-jar</artifactId>
</project>
有一个war pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>parentpom</artifactId>
<groupId>com.shagie.mvn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>the-war</artifactId>
<dependencies>
<dependency>
<groupId>com.shagie.mvn</groupId>
<artifactId>the-jar</artifactId>
</dependency>
</dependencies>
</project>
还有父pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.shagie.mvn</groupId>
<artifactId>parentpom</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>the-jar</module>
<module>the-war</module>
</modules>
<packaging>pom</packaging>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.shagie.mvn</groupId>
<artifactId>the-jar</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>jdk8</id>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>jdk8</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<classifier>jdk8</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.shagie.mvn</groupId>
<artifactId>the-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>jdk8</classifier>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
</profiles>
</project>
各个工件确实使用指定的 Java 版本正确构建。
不幸的是,战争并没有使用指定 jar 作为依赖项的 jdk8 分类器来构建。
mvn help:effective-pom -P jdk8
在war项目中运行时可以看到:
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.shagie.mvn</groupId>
<artifactId>the-jar</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.shagie.mvn</groupId>
<artifactId>the-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>jdk8</classifier>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.shagie.mvn</groupId>
<artifactId>the-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
...
这反映在运行依赖图时:
$ mvn dependency:tree -P jdk8
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building the-war 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ the-war ---
[INFO] com.shagie.mvn:the-war:jar:1.0-SNAPSHOT
[INFO] \- com.shagie.mvn:the-jar:jar:1.0-SNAPSHOT:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
有没有办法让配置文件激活来更改特定的 jar 依赖项以使用分类器?