我正在尝试为涉及外部依赖项的 Maven 项目构建多版本 jar。我之所以强调“外部依赖”是因为我按照这里的说明成功构建了多版本 jar:http: //in.relation.to/2017/02/13/building-multi-release-jars-with -maven/用于在 pom.xml 中没有外部依赖项的项目。当我将此项目更改为在 pom 中具有依赖项时,我收到错误消息:包不存在。报告的包存在于包含的依赖项中。这是项目结构:
Pom.xml:
<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>org.hibernate.demos</groupId>
<artifactId>multi-release-jar-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>multi-release-jar-demo</name>
<url>http://hibernate.org/</url>
<properties>
<commonslang.version>3.8.1</commonslang.version>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java9.sourceDirectory>${project.basedir}/src/main/java9</java9.sourceDirectory>
<java9.build.outputDirectory>${project.build.directory}/classes-java9</java9.build.outputDirectory>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>compile-java9</id>
<phase>compile</phase>
<configuration>
<tasks>
<mkdir dir="${java9.build.outputDirectory}" />
<javac srcdir="${java9.sourceDirectory}" destdir="${java9.build.outputDirectory}"
classpath="${project.build.outputDirectory}" includeantruntime="false" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/META-INF/versions/9</outputDirectory>
<resources>
<resource>
<directory>${java9.build.outputDirectory}</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
<!--<Main-Class>org.hibernate.demos.mrjar.Main</Main-Class>-->
</manifestEntries>
</archive>
<!--<finalName>mr-jar-demo</finalName>-->
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>initialize</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
<scope>provided</scope>
</dependency>
</dependencies>
src/main/java9/org/hibernate/demos/mrjar/ProcessIdProvider.java:
public class ProcessIdProvider {
public ProcessIdDescriptor getPid() {
Configuration properties = null;
long pid = ProcessHandle.current().pid();
return new ProcessIdDescriptor( pid, "ProcessHandle" );
}
}
错误:
Documents/Development/hibernate-demos/java9/multi-release-jar-demo/src/main/java9/org/hibernate/demos/mrjar/ProcessIdProvider.java:7: error: package org.apache.commons.configuration does not exist
import org.apache.commons.configuration.Configuration;
^
/Documents/Development/hibernate-demos/java9/multi-release-jar-demo/src/main/java9/org/hibernate/demos/mrjar/ProcessIdProvider.java:17: error: cannot find symbol
Configuration properties = null;
^
symbol: class Configuration
location: class ProcessIdProvider
请注意,此错误仅出现在文件夹 src/main/java9 中的类,并且 src/main/java 中的文件编译成功。我似乎没有找到任何具有构建多版本 jar 的依赖项的示例 maven 项目。