对于Maven Central(或任何其他给定的Nexus存储库)上的工件,我想列出所有直接依赖项。
最初,我考虑过只阅读pom.xml并从依赖项部分收集所有条目。但是我注意到这些条目可能没有版本(由依赖管理提供),或者这些条目可能来自父poms。
我的第二个想法是建立某种人造Maven项目并使用收集相关性mvn dependency:tree
。这可能变得复杂。
什么是最直接(也是可靠)的方法?
在Maven插件之外,您可以使用Aether以编程方式执行此操作。有一种方法readArtifactDescriptor
可以做到这一点:
获取有关工件的信息,例如其直接依赖关系和潜在的重定位。
首先,将以太依赖项添加到您的POM中:
<dependencies>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-file</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-http</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<version>${mavenVersion}</version>
</dependency>
</dependencies>
<properties>
<aetherVersion>1.1.0</aetherVersion>
<mavenVersion>3.3.9</mavenVersion>
</properties>
然后您可以拥有:
public static void main(final String[] args) throws Exception {
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
RepositorySystem system = newRepositorySystem(locator);
RepositorySystemSession session = newSession(system);
RemoteRepository central = new RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2/").build();
Artifact artifact = new DefaultArtifact("groupId:artifactId:version");
ArtifactDescriptorRequest request = new ArtifactDescriptorRequest(artifact, Arrays.asList(central), null);
ArtifactDescriptorResult result = system.readArtifactDescriptor(session, request);
for (Dependency dependency : result.getDependencies()) {
System.out.println(dependency);
}
}
private static RepositorySystem newRepositorySystem(DefaultServiceLocator locator) {
locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
locator.addService(TransporterFactory.class, FileTransporterFactory.class);
locator.addService(TransporterFactory.class, HttpTransporterFactory.class);
return locator.getService(RepositorySystem.class);
}
private static RepositorySystemSession newSession(RepositorySystem system) {
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository localRepo = new LocalRepository("target/local-repo");
session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
// set possible proxies and mirrors
session.setProxySelector(new DefaultProxySelector().add(new Proxy(Proxy.TYPE_HTTP, "host", 3625), Arrays.asList("localhost", "127.0.0.1")));
session.setMirrorSelector(new DefaultMirrorSelector().add("my-mirror", "http://mirror", "default", false, "external:*", null));
return session;
}
这是创建一个以太存储库系统,并告诉它读取给定工件的工件描述符。工件由构造函数构造,new DefaultArtifact("...")
并为其提供所需的坐标。
使用该工件和要从中获取请求的存储库列表创建一个请求对象。在上面的示例中,仅添加了Maven
Central,但是您可以RemoteRepository
通过使用RemoteRepository.Builder
类创建它们来添加更多内容。调用后readArtifactDescriptor
,结果包含直接依赖项列表,可以使用检索getDependencies()
。
代理和镜像可以分别在DefaultProxySelector
和的帮助下进行配置DefaultMirrorSelector
。DefaultProxySelector.add
接受一个Proxy
as参数,可以使用其构造函数通过将其类型(例如"http"
),主机,端口和可选的Authentication
(通过查看AuthenticationBuilder
类以创建身份验证对象)和非代理主机的列表传递给它。以相同的方式,DefaultMirrorSelector.add
获取其ID,URL,类型("default"
适用于Maven,但您可以使用P2),是否为存储库管理器,已镜像的实际存储库ID(根据镜像规范)以及存储库类型未镜像。
问题内容: 我有一个python脚本(2.7),顶部有一些“有趣的”导入。我最初想使用py2exe将其编译成一个我可以更容易分发的exe。 我已经放弃,正在尝试使用cx- freeze。但是,我在那里也有问题。问题似乎是我添加到Python中的库(jinja2和restkit)。我在python目录./Lib/site-packages/Jinja2-2.6-py2.7.egg/jinja2和这里
问题内容: 我正在尝试获取Java类中的所有依赖关系,包括用于泛型参数化和局部变量类型的类。到目前为止,我发现的最佳框架是apache bcel。使用它,我可以轻松地从字节码中找到所有字段,方法参数和局部变量。基本上,除了泛型和局部变量类型以外的所有内容。例如,在第1行中,我只能使用bcel中的方法找到一个依赖项- ArrayList。它无法检测到List接口和Point类。不幸的是,我也尝试了t
构建一个Java /Groovy项目,各种任务,如编译Java、编译Groovy、测试等,需要各种jar工件,如果您正确定义了该任务的每个区域所需的内容,Gradle会提供这些工件。 我将这些应用于构建脚本(build.gradle ),一切正常。 我正在处理的另一个项目不仅需要jar rtifacts,还需要一个.xml文件作为进行JIBX / XSLT转换/处理的工件。 我的简单问题: - G
我试图用生成一个由多个子模块组成的项目的Java-API文档,但它不起作用,而起作用,它解决了所有依赖项,并成功编译<代码>mvn站点既不解析父pom文件(包括easymock)中所有子模块继承的依赖项,也不解析特定于某些子模块(包括SWT)的依赖项。 我获得以下错误消息:[ERROR]未能执行目标org.apache.maven.plugins:maven-site-plugin:3.7.1:站
我已经尝试重新启动我的node_dependencies并再次运行,但我不确定还可以尝试什么 编辑:我已经验证了我的node_dependencies目录和插件目录在那里。
我正试图让maven下载所有的依赖项(编译、测试、插件等)。)这样我就可以避免让我们的dockerized构建浪费不必要的时间一遍又一遍地下载它们。 我们已经对maven build进行了dockerized,这样我们就可以从jenkins运行它,而无需在jenkins机器上安装大量构建特定的依赖项(Java、redis、maven依赖项等)。我们的构建依赖于增量docker构建,它只执行实际需要