2

我正在尝试为涉及外部依赖项的 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 项目。

4

1 回答 1

0

我用SL4j使用的相同方法解决了问题,我使用他们pom.xml中的相同配置,在我的例子中是 java 11 和 12。

属性:

<properties>
  <maven.compiler.source>11</maven.compiler.source>
  <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
  <java12-outputdirectory>${project.build.outputDirectory}/META-INF/versions/12</java12-outputdirectory>
  <java12-build-directory>${project.build.directory}/classes-java12</java12-build-directory>
  <maven-jar-plugin.version>2.3.1</maven-jar-plugin.version>
</properties>

构建部分:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>${maven.compiler.version}</version>
      <executions>
        <execution>
          <id>default-compile</id>
          <goals>
            <goal>compile</goal>
          </goals>
          <configuration>
            <source>12</source>
            <target>12</target>
          </configuration>
        </execution>
        <execution>
          <id>module-compile</id>
          <phase>compile</phase>
          <goals>
            <goal>compile</goal>
          </goals>
          <configuration>
            <release>12</release>
            <compileSourceRoots>
              <compileSourceRoot>${project.basedir}/src/main/java12</compileSourceRoot>
            </compileSourceRoots>
            <outputDirectory>${project.build.outputDirectory}/META-INF/versions/12
            </outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>${maven-jar-plugin.version}</version>
      <configuration>
        <archive>
          <manifestEntries>
            <Multi-Release>true</Multi-Release>
          </manifestEntries>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

文件夹结构: 文件夹结构示例

完整的例子在这里

于 2020-02-06T20:12:23.757 回答