为涉及依赖项的项目构建多版本jar

时间:2018-11-28 16:53:11

标签: java maven java-9 multi-release-jar

我正在尝试为涉及外部依赖项的maven项目构建多发行版jar。我之所以强调“外部依赖项”,是因为我成功按照以下说明构建了多发行版jar:http://in.relation.to/2017/02/13/building-multi-release-jars-with-maven/用于pom中没有外部依赖项的项目。当我将此项目更改为在pom中具有依赖项时,出现错误消息:软件包不存在。报告的程序包存在于包含的依赖项中。这是项目结构:

Project structure

Pom.xml:

regModel.set_weights(model.get_weights())

src / main / java9 / org / hibernate / demos / mrjar / ProcessIdProvider.java:

<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>

错误:

public class ProcessIdProvider {
    public ProcessIdDescriptor getPid() {
        Configuration properties = null;
        long pid = ProcessHandle.current().pid();
        return new ProcessIdDescriptor( pid, "ProcessHandle" );
    }
}

请注意,此错误仅对驻留在src / main / java9文件夹中的类出现,并且src / main / java内的文件成功编译。我似乎找不到任何具有构建多版本jar的依赖关系的示例Maven项目。

1 个答案:

答案 0 :(得分:0)

我用SL4j使用的相同方法解决了问题,在Java 11和12中,我使用了pom.xml的相同配置。

属性:

<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>

文件夹结构: Folder structure example

完整的示例是here