关于jcabi Aether的main web page是一个在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)
别误会我的意思。听起来像一个伟大的项目。但它确实有效吗?首页上的示例是什么?有人有经验吗?
答案 0 :(得分:1)
在https://github.com/jcabi/jcabi-aether/blob/master/src/main/java/com/jcabi/aether/Aether.java的jcabi-aether的github代码中,我们确实需要添加以下两个依赖项:
我添加了这些并获得了运行的示例代码
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>