2

在 jcabi Aether 的主页上是一个在 Maven 之外使用的简单示例,它需要一个 Maven 依赖项(jcabi-aether 0.9)。我已经为那个“Main”创建了一个项目,只是为了尝试一下,但它似乎有一些,嗯,困难。

public class Main {
  public static void main(String[] args) {
    File local = new File("/tmp/local-repository");
    Collection<RemoteRepository> remotes = Arrays.asList(
      new RemoteRepository(
        "maven-central",
        "default",
        "http://repo1.maven.org/maven2/"
      )
    );
    Collection<Artifact> deps = new Aether(remotes, local).resolve( // #1
      new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
      "runtime"
    );
  }
}

对于初学者来说,#1 会产生这个错误(来自 Eclipse):

该项目未构建,因为其构建路径不完整。找不到 org.apache.maven.project.MavenProject 的类文件。修复构建路径,然后尝试构建此项目

所以......没什么大不了的,我想:我只是折腾 maven-project (2.2.1)。嘿,它编译!伟大的!但是等一下,运行它时:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/maven/settings/building/SettingsBuildingException
    at org.test.aether.Main.main(Main.java:23)

嗯。好的,所以我也会加入 maven-settings-builder (3.3.2)。现在我们应该可以走了……对吧?好吧,不:这一次,我得到了大量的异常输出,如下所示:

java.lang.instrument.IllegalClassFormatException: Error while instrumenting class com/jcabi/aether/Aether.
    at org.jacoco.agent.rt.internal_9dd1198.CoverageTransformer.transform(CoverageTransformer.java:89)
    at sun.instrument.TransformerManager.transform(TransformerManager.java:169)
    at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:365)

不要误会我的意思。听起来是个很棒的项目。但它真的有效吗?头版上的例子是怎么回事?有人有这方面的经验吗?

4

1 回答 1

1

https://github.com/jcabi/jcabi-aether/blob/master/src/main/java/com/jcabi/aether/Aether.java的 jcabi-aether 的 github 代码,我们确实需要添加以下两个依赖项:

  • org.sonatype.aether:aether-api:1.13.1
  • org.apache.maven:maven-core:3.0.3

我添加了这些并让示例代码运行

import java.io.File;
import java.util.Arrays;
import java.util.Collection;

import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.resolution.DependencyResolutionException;
import org.sonatype.aether.util.artifact.DefaultArtifact;

import com.jcabi.aether.Aether;

public class RunAether {

public static void main(String[] args) throws DependencyResolutionException {

    File local = new File("/tmp/local-repository");
    Collection<RemoteRepository> remotes = Arrays.asList(
      new RemoteRepository(
        "maven-central",
        "default",
        "http://repo1.maven.org/maven2/"
      )
    );
    Collection<Artifact> deps = new Aether(remotes, local).resolve(
      new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
      "runtime"
    );

    System.out.println("Got the Junit dependencies");
}

}

我的 pom 文件依赖项:

<dependency>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-aether</artifactId>
        <version>0.10</version>
    </dependency>

    <dependency>
        <groupId>org.sonatype.aether</groupId>
        <artifactId>aether-api</artifactId>
        <version>1.13.1</version>
    </dependency>


    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-core</artifactId>
        <version>3.0.3</version>
    </dependency>
于 2015-06-29T21:38:08.547 回答