如何使用maven获取依赖jar的路径

时间:2015-01-23 08:14:46

标签: java maven dependencies

给定一个简单的Maven项目,例如JUnit作为依赖项,如何将完整的文件路径提供给它将安装到的本地maven存储库中的junit.jar?!

e.g。如何从工件junit:junit/Users/foobar/.m2/repository/junit/junit/4.11/junit-4.11.jar

5 个答案:

答案 0 :(得分:4)

看起来最简单的方法是使用antrun插件 https://stackoverflow.com/a/2359947/2055199

<plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>process-resources</phase> <configuration> <tasks> <echo>${maven.dependency.junit.junit.jar.path}</echo> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>

答案 1 :(得分:2)

hacky解决方案使用mvn dependency:build-classpath和一些unix shell magic从类路径中提取jar路径。

我们有pom.xml这样......

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany</groupId>
  <artifactId>myproject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

然后我们生成一个build_classpath文件。

$ mvn dependency:build-classpath -Dmdep.outputFile=build_classpath
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:build-classpath (default-cli) @ myproject ---
[INFO] Wrote classpath file '/Users/foobar/maven-test/build_classpath'.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.050 s
[INFO] Finished at: 2015-01-23T09:17:40+01:00
[INFO] Final Memory: 11M/245M
[INFO] ------------------------------------------------------------------------
$ cat build_classpath
/Users/foobar/.m2/repository/junit/junit/4.11/junit-4.11.jar:/Users/foobar/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar

现在我们可以使用一些脚本foo ...

build_classpath中提取jar文件路径
$ cat build_classpath | perl -ne 'print "$1" if /(?:^|:)([^:]+?\/junit-[0-9\.]+\.jar)/'
/Users/foobar/.m2/repository/junit/junit/4.11/junit-4.11.jar

答案 2 :(得分:0)

路径构建为$ repository_dir / groupId / artifactId / version / artifactId-version.jar

<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>

答案 3 :(得分:0)

如果要获取正在运行的代码中的路径,可以执行以下操作:

POM:

    <dependency>
        <groupId>com.github.nodyn</groupId>
        <artifactId>jvm-npm</artifactId>
        <version>a0c3f12</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.aether</groupId>
        <artifactId>aether-impl</artifactId>
        <version>1.1.0</version>
    </dependency>
            <dependency>
        <groupId>org.eclipse.aether</groupId>
        <artifactId>aether-transport-file</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.aether</groupId>
        <artifactId>aether-connector-basic</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-aether-provider</artifactId>
        <version>3.1.0</version>
    </dependency>

代码:

final String mavenRepositoryPath = "c:\\mvn\\repository";

private static RepositorySystem newRepositorySystem() {
    DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
    locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
    locator.addService(TransporterFactory.class, FileTransporterFactory.class);

    locator.setErrorHandler(new DefaultServiceLocator.ErrorHandler() {
        @Override
        public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable exception) {
            throw new RuntimeException(exception);
        }
    });

    return locator.getService(RepositorySystem.class);
}

private static File getJvmNpmFile() {
    Artifact artifact = new DefaultArtifact("com.github.nodyn:jvm-npm:a0c3f12");

    DefaultRepositorySystemSession session = new org.eclipse.aether.DefaultRepositorySystemSession();
    RepositorySystem system = newRepositorySystem();
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(artifact);
    //request.setRepositories(new ArrayList<>( Arrays.asList( new RemoteRepository.Builder( "central", "default", "http://central.maven.org/maven2/" ).build()) ));
    //request.setRepositories( new org.eclipse.aether.DefaultRepositorySystemSession().getLocalRepository() );
    LocalRepository localRepo = new LocalRepository(mvnRepositoryPath);
    session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));

    ArtifactResult result;
    try {
        result = system.resolveArtifact(session, request);
    } catch (ArtifactResolutionException ex) {
        throw new RuntimeException(ex);
    }

    //System.out.println("Resolved artifact " + artifact + " to " + result.getArtifact().getFile() + " from " + result.getRepository());
    return result.getArtifact().getFile();
}

答案 4 :(得分:0)

maven依赖插件具有目标“属性”。来自documentation

  

为每个项目依赖项设置指向工件文件的属性的目标。对于每个依赖项(直接和传递),将设置一个项目属性,该属性遵循groupId:artifactId:type:[classifier]形式,并包含已解析工件的路径。

所以这样的事情应该可以解决问题:

<properties>
    <maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
</properties>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>${maven-dependency-plugin.version}</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>properties</goal>
            </goals>
        </execution>
    </executions>
</plugin>

然后,属性${junit:junit:jar}应该包含jar文件路径

相关问题