【问题标题】:Maven nar plugin: nar file not in classpath of dependent projectMaven nar插件:nar文件不在依赖项目的类路径中
【发布时间】:2020-01-27 20:18:09
【问题描述】:

我有一个用 C 语言编写的 JNI 共享库和一个 maven 项目中的相应 Java 类,以及在另一个项目中使用该类的应用程序。我使用 nar-maven-plugin 来处理 JNI。 版本: Maven 3.6.0 爪哇 12 nar 插件 3.6.0 我以 nar-maven-plugin 附带的集成测试 3 和 4 为例。 对于它 3,我刚刚将 POM 更新为独立的并与 Java 12 一起使用:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  #%L
  Native ARchive plugin for Maven
  %%
  Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
  %%
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  #L%
  -->

<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.github.maven-nar</groupId>
  <artifactId>it0003-jni</artifactId>
  <packaging>nar</packaging>

  <name>NAR JNI Test</name>
  <version>1.0-SNAPSHOT</version>
  <description>
    Simple JNI Library
  </description>
  <url>http://maven.apache.org/</url>

  <properties>
    <skipTests>true</skipTests>
    <maven-compiler.version>3.8.1</maven-compiler.version>
    <surefire.version>2.22.2</surefire.version>
    <java.version>12</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <defaultGoal>install</defaultGoal>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler.version}</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
          <compilerArgs>
            <arg>-Xlint</arg>
            <arg>-h</arg>
            <arg>${project.build.directory}/nar/javah-include</arg>
          </compilerArgs>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${surefire.version}</version>
        <configuration>
          <excludes>
            <exclude>**/*Test.java</exclude>
          </excludes>
          <argLine>
            <!-- - -illegal-access=permit-->
          </argLine>
        </configuration>
      </plugin>
      <plugin>
        <groupId>com.github.maven-nar</groupId>
        <artifactId>nar-maven-plugin</artifactId>
        <version>3.6.0</version>
        <extensions>true</extensions>
        <configuration>
          <cpp>
            <debug>true</debug>
          </cpp>
          <c>
            <testOptions>
              <testOption>-DTESTOPT="this is a nar-testCompile flag"</testOption>
            </testOptions>
          </c>
          <libraries>
            <library>
              <type>jni</type>
              <narSystemPackage>it0003</narSystemPackage>
              <linkCPP>false</linkCPP>
            </library>
          </libraries>
          <javah>
            <includes>
              <include></include>
            </includes>
          </javah>
          <tests>
            <test>
              <name>HelloWorld</name>
            </test>
          </tests>
        </configuration>
        <executions> 
          <execution>
            <id>default-nar-javah</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

我在这个项目上运行mvn install,它安装在我的.m2 目录中

/home/karsten/.m2/repository/com/github/maven-nar/it0003-jni/:
total 0
drwxr-xr-x 1 karsten karsten 4096 Sep 27 12:20 1.0-SNAPSHOT
-rw-r--r-- 1 karsten karsten  288 Sep 27 12:21 maven-metadata-local.xml

/home/karsten/.m2/repository/com/github/maven-nar/it0003-jni/1.0-SNAPSHOT:
total 20
-rw-r--r-- 1 karsten karsten  250 Sep 27 12:21 _remote.repositories
-rw-r--r-- 1 karsten karsten 3097 Sep 27 12:21 it0003-jni-1.0-SNAPSHOT-amd64-Linux-gpp-jni.nar
-rw-r--r-- 1 karsten karsten 4570 Sep 27 12:21 it0003-jni-1.0-SNAPSHOT.nar
-rw-r--r-- 1 karsten karsten 4086 Sep 27 12:21 it0003-jni-1.0-SNAPSHOT.pom
-rw-r--r-- 1 karsten karsten  925 Sep 27 12:21 maven-metadata-local.xml

it0003-jni-1.0-SNAPSHOT.nar 包含

META-INF/MANIFEST.MF
META-INF/
META-INF/nar/
META-INF/nar/com.github.maven-nar/
META-INF/nar/com.github.maven-nar/it0003-jni/
it0003/
META-INF/maven/
META-INF/maven/com.github.maven-nar/
META-INF/maven/com.github.maven-nar/it0003-jni/
META-INF/nar/com.github.maven-nar/it0003-jni/nar.properties
it0003/HelloWorldJNI.class
it0003/NarSystem.class
META-INF/maven/com.github.maven-nar/it0003-jni/pom.xml
META-INF/maven/com.github.maven-nar/it0003-jni/pom.properties

现在我用它4的精简版来测试它,这是Java文件src/main/java/it0004/Hello.java

package it0004;

import it0003.HelloWorldJNI;

public class Hello {
    public static void hello()
    {
        HelloWorldJNI.sayHello();
    }
}

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <properties>
    <java.version>12</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven-compiler.version>3.8.1</maven-compiler.version>
    <maven-source.version>2.0.4</maven-source.version>
    <nar-plugin.version>3.6.0</nar-plugin.version>
  </properties>
  <groupId>mygroup</groupId>
  <artifactId>myit0004</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0</version>
  <name>Java dependency test</name>
  <dependencies>
    <dependency>
      <groupId>com.github.maven-nar</groupId>
      <artifactId>it0003-jni</artifactId>
      <version>1.0-SNAPSHOT</version>
      <type>nar</type>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler.version}</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
          <compilerArgs>
            <arg>-Xlint</arg>
          </compilerArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

请注意,此 POM 仅处理编译。使用&lt;type&gt;nar&lt;/type&gt; 指定依赖关系,以便找到依赖关系。但是nar文件不在classPath中,所以编译失败。 mvn -X compilecontains 的输出

[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ myit0004 ---
[DEBUG] Dependency collection stats: {ConflictMarker.analyzeTime=428200, ConflictMarker.markTime=241300, ConflictMarker.nodeCount=118, ConflictIdSorter.graphTime=137600, ConflictIdSorter.topsortTime=62600, ConflictIdSorter.conflictIdCount=45, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=4391200, ConflictResolver.conflictItemCount=72, DefaultDependencyCollector.collectTime=419330500, DefaultDependencyCollector.transformTime=5343800}
[DEBUG] org.apache.maven.plugins:maven-compiler-plugin:jar:3.8.1:
[DEBUG]    org.apache.maven:maven-plugin-api:jar:3.0:compile
[DEBUG]       org.apache.maven:maven-model:jar:3.0:compile
[DEBUG]       org.sonatype.sisu:sisu-inject-plexus:jar:1.4.2:compile
[DEBUG]          org.sonatype.sisu:sisu-inject-bean:jar:1.4.2:compile
[DEBUG]             org.sonatype.sisu:sisu-guice:jar:noaop:2.1.7:compile
[DEBUG]    org.apache.maven:maven-artifact:jar:3.0:compile
[DEBUG]       org.codehaus.plexus:plexus-utils:jar:2.0.4:compile
[DEBUG]    org.apache.maven:maven-core:jar:3.0:compile
[DEBUG]       org.apache.maven:maven-settings:jar:3.0:compile
[DEBUG]       org.apache.maven:maven-settings-builder:jar:3.0:compile
[DEBUG]       org.apache.maven:maven-repository-metadata:jar:3.0:compile
[DEBUG]       org.apache.maven:maven-model-builder:jar:3.0:compile
[DEBUG]       org.apache.maven:maven-aether-provider:jar:3.0:runtime
[DEBUG]       org.sonatype.aether:aether-impl:jar:1.7:compile
[DEBUG]          org.sonatype.aether:aether-spi:jar:1.7:compile
[DEBUG]       org.sonatype.aether:aether-api:jar:1.7:compile
[DEBUG]       org.sonatype.aether:aether-util:jar:1.7:compile
[DEBUG]       org.codehaus.plexus:plexus-interpolation:jar:1.14:compile
[DEBUG]       org.codehaus.plexus:plexus-classworlds:jar:2.2.3:compile
[DEBUG]       org.codehaus.plexus:plexus-component-annotations:jar:1.7.1:compile
[DEBUG]       org.sonatype.plexus:plexus-sec-dispatcher:jar:1.3:compile
[DEBUG]          org.sonatype.plexus:plexus-cipher:jar:1.4:compile
[DEBUG]    org.apache.maven.shared:maven-shared-utils:jar:3.2.1:compile
[DEBUG]       commons-io:commons-io:jar:2.5:compile
[DEBUG]    org.apache.maven.shared:maven-shared-incremental:jar:1.1:compile
[DEBUG]    org.codehaus.plexus:plexus-java:jar:0.9.10:compile
[DEBUG]       org.ow2.asm:asm:jar:6.2:compile
[DEBUG]       com.thoughtworks.qdox:qdox:jar:2.0-M9:compile
[DEBUG]    org.codehaus.plexus:plexus-compiler-api:jar:2.8.4:compile
[DEBUG]    org.codehaus.plexus:plexus-compiler-manager:jar:2.8.4:compile
[DEBUG]    org.codehaus.plexus:plexus-compiler-javac:jar:2.8.4:runtime
[DEBUG] Created new class realm plugin>org.apache.maven.plugins:maven-compiler-plugin:3.8.1
[DEBUG] Importing foreign packages into class realm plugin>org.apache.maven.plugins:maven-compiler-plugin:3.8.1
[DEBUG]   Imported:  < maven.api
[DEBUG] Populating class realm plugin>org.apache.maven.plugins:maven-compiler-plugin:3.8.1
[DEBUG]   Included: org.apache.maven.plugins:maven-compiler-plugin:jar:3.8.1
[DEBUG]   Included: org.sonatype.sisu:sisu-inject-bean:jar:1.4.2
[DEBUG]   Included: org.sonatype.sisu:sisu-guice:jar:noaop:2.1.7
[DEBUG]   Included: org.codehaus.plexus:plexus-utils:jar:2.0.4
[DEBUG]   Included: org.sonatype.aether:aether-util:jar:1.7
[DEBUG]   Included: org.codehaus.plexus:plexus-interpolation:jar:1.14
[DEBUG]   Included: org.codehaus.plexus:plexus-component-annotations:jar:1.7.1
[DEBUG]   Included: org.sonatype.plexus:plexus-sec-dispatcher:jar:1.3
[DEBUG]   Included: org.sonatype.plexus:plexus-cipher:jar:1.4
[DEBUG]   Included: org.apache.maven.shared:maven-shared-utils:jar:3.2.1
[DEBUG]   Included: commons-io:commons-io:jar:2.5
[DEBUG]   Included: org.apache.maven.shared:maven-shared-incremental:jar:1.1
[DEBUG]   Included: org.codehaus.plexus:plexus-java:jar:0.9.10
[DEBUG]   Included: org.ow2.asm:asm:jar:6.2
[DEBUG]   Included: com.thoughtworks.qdox:qdox:jar:2.0-M9
[DEBUG]   Included: org.codehaus.plexus:plexus-compiler-api:jar:2.8.4
[DEBUG]   Included: org.codehaus.plexus:plexus-compiler-manager:jar:2.8.4
[DEBUG]   Included: org.codehaus.plexus:plexus-compiler-javac:jar:2.8.4
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-compiler-plugin:3.8.1, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@446cdf90]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile' with basic configurator -->
[DEBUG]   (f) basedir = /home/karsten/svn/myit0004
[DEBUG]   (f) buildDirectory = /home/karsten/svn/myit0004/target
[DEBUG]   (f) compilePath = [/home/karsten/svn/myit0004/target/classes]
[DEBUG]   (f) compileSourceRoots = [/home/karsten/svn/myit0004/src/main/java]
[DEBUG]   (f) compilerArgs = [-Xlint]
[DEBUG]   (f) compilerId = javac
[DEBUG]   (f) debug = true
[DEBUG]   (f) encoding = UTF-8
[DEBUG]   (f) failOnError = true
[DEBUG]   (f) failOnWarning = false
[DEBUG]   (f) forceJavacCompilerUse = false
[DEBUG]   (f) fork = false
[DEBUG]   (f) generatedSourcesDirectory = /home/karsten/svn/myit0004/target/generated-sources/annotations
[DEBUG]   (f) mojoExecution = org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile {execution: default-compile}
[DEBUG]   (f) optimize = false
[DEBUG]   (f) outputDirectory = /home/karsten/svn/myit0004/target/classes
[DEBUG]   (f) parameters = false
[DEBUG]   (f) project = MavenProject: mygroup:myit0004:1.0.0 @ /home/karsten/svn/myit0004/pom.xml
[DEBUG]   (f) projectArtifact = mygroup:myit0004:jar:1.0.0
[DEBUG]   (f) session = org.apache.maven.execution.MavenSession@3e84111a
[DEBUG]   (f) showDeprecation = false
[DEBUG]   (f) showWarnings = false
[DEBUG]   (f) skipMultiThreadWarning = false
[DEBUG]   (f) source = 12
[DEBUG]   (f) staleMillis = 0
[DEBUG]   (s) target = 12
[DEBUG]   (f) useIncrementalCompilation = true
[DEBUG]   (f) verbose = false
[DEBUG] -- end configuration --
[DEBUG] Using compiler 'javac'.
[DEBUG] Adding /home/karsten/svn/myit0004/target/generated-sources/annotations to compile source roots:
  /home/karsten/svn/myit0004/src/main/java
[DEBUG] New compile source roots:
  /home/karsten/svn/myit0004/src/main/java
  /home/karsten/svn/myit0004/target/generated-sources/annotations
[DEBUG] CompilerReuseStrategy: reuseCreated
[DEBUG] useIncrementalCompilation enabled
[DEBUG] Stale source detected: /home/karsten/svn/myit0004/src/main/java/it0004/Hello.java
[INFO] Changes detected - recompiling the module!
[DEBUG] Classpath:
[DEBUG]  /home/karsten/svn/myit0004/target/classes
[DEBUG] Source roots:
[DEBUG]  /home/karsten/svn/myit0004/src/main/java
[DEBUG]  /home/karsten/svn/myit0004/target/generated-sources/annotations
[DEBUG] Command line options:
[DEBUG] -d /home/karsten/svn/myit0004/target/classes -classpath /home/karsten/svn/myit0004/target/classes: -sourcepath /home/karsten/svn/myit0004/src/main/java:/home/karsten/svn/myit0004/target/generated-sources/annotations: -s /home/karsten/svn/myit0004/target/generated-sources/annotations -g -nowarn -target 12 -source 12 -encoding UTF-8 -Xlint
[DEBUG] incrementalBuildHelper#beforeRebuildExecution
[INFO] Compiling 1 source file to /home/karsten/svn/myit0004/target/classes
[DEBUG] incrementalBuildHelper#afterRebuildExecution
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/karsten/svn/myit0004/src/main/java/it0004/Hello.java:[3,14] package it0003 does not exist
[ERROR] /home/karsten/svn/myit0004/src/main/java/it0004/Hello.java:[8,9] cannot find symbol
  symbol:   variable HelloWorldJNI
  location: class it0004.Hello
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.606 s
[INFO] Finished at: 2019-09-27T13:28:44Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project myit0004: Compilation failure: Compilation failure:
[ERROR] /home/karsten/svn/myit0004/src/main/java/it0004/Hello.java:[3,14] package it0003 does not exist
[ERROR] /home/karsten/svn/myit0004/src/main/java/it0004/Hello.java:[8,9] cannot find symbol
[ERROR]   symbol:   variable HelloWorldJNI
[ERROR]   location: class it0004.Hello

可以看到,依赖的nar文件不在classPath中,所以编译失败,因为找不到依赖类。如何确保 nar 文件包含在 classPath 中?

【问题讨论】:

    标签: java maven dependencies classpath maven-nar-plugin


    【解决方案1】:

    找到了答案。我应该查看 it0025 示例。需要添加到依赖项目的pom.xml的是

          <plugin>
            <groupId>com.github.maven-nar</groupId>
            <artifactId>nar-maven-plugin</artifactId>
            <version>${nar-plugin.version}</version>
            <extensions>true</extensions>
            <executions>
              <execution>
                <id>nar-download</id>
                <goals>
                  <goal>nar-download</goal>
                </goals>
              </execution>
              <execution>
                <id>nar-test-unpack</id>
                <goals>
                  <goal>nar-test-unpack</goal>
                </goals>
              </execution>
              <execution>
                <id>nar-integration-test</id>
                <goals>
                  <goal>nar-integration-test</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
    

    【讨论】:

      猜你喜欢
      • 2012-12-04
      • 2021-09-01
      • 2017-05-06
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多