以编程方式使用Maven [英] Using maven programmatically

查看:129
本文介绍了以编程方式使用Maven的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要制作一个小程序来下载maven项目并打印其依赖项

类似这样的东西:

MavenArtifactRepository repository = new MavenArtifactRepository("typesafe", "http://repo.typesafe.com/typesafe/releases/", ..., ..., ...);
downloadAndPrintDependencies(repository, "org.hsqldb", "hsqldb", "2.2.9");

void downloadAndPrintDependencies(repository, groupId, artifactId, version) {
  MavenProject projectDescription = new MavenProject("org.hsqldb", "hsqldb", "2.2.9");
  Artifact artifact = repository.getProject(projectDescription);  // this would download the artificat in the local repository if necessary

  List<Dependency> dependecies = artifact.getDependencies();
  ...
}

并且可以在Maven项目上执行目标,如下所示:

String pomXmlFile = "/tmp/myproject/pom.xml";
Reader reader = new FileReader(pomXmlFile);
MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
Model model = xpp3Reader.read(reader);

ProjectArtifact projectArtifact = new ProjectArtifact(model);
projectArtifact.clean();
projectArtifact.install();


对伪代码有任何反馈吗?

从存储库中获取工件的正确类和函数是什么?

在maven项目上执行目标(例如清理和安装)的正确类和函数是什么?

解决方案

善良

我有一个项目Naether,是Maven依赖项解决方案库Aether的包装. >

使用Naether,您可以解决依赖关系

import com.tobedevoured.naether.api.Naether;
import com.tobedevoured.naether.impl.NaetherImpl;

Naether naether = new NaetherImpl();
naether.addDependency( "ch.qos.logback:logback-classic:jar:0.9.29" );
naether.addDependency( "junit:junit:jar:4.8.2" );
naether.resolveDependencies();
System.out.println( naether.getDependenciesNotation().toString() );

将输出:

["ch.qos.logback:logback-core:jar:0.9.29",
 "ch.qos.logback:logback-classic:jar:0.9.29",
 "junit:junit:jar:4.8.2",
 "org.slf4j:slf4j-api:jar:1.6.1" ]

坏人

我不知道如何通过Java构建(例如编译源代码)pom.xml.我进行了一些搜索,但没有找到具体的例子. ProjectArtifact 只是Maven用来解析POM(例如父POM)的工件描述符.它不公开构建操作.由于有上百万种构建Maven项目的方法,因此没有简单的 install 方法.您必须以某种方式启动安装过程的生命周期.

Naether可以做什么,首先构建项目并拥有


更新-它们如何组合在一起?

构建,部署,安装项目等非常复杂. Maven在简化它方面做得很好.即使Maven任务只是 install ,也要执行许多步骤.对于一个简单的Java项目,这意味着填充类路径,编译源代码,打包jar,然后将其安装在本地Maven存储库中.当您谈论打包Java项目的其他方式(例如 war )时,事情只会变得更加复杂.

Maven的人做了艰苦的工作,并将依赖项解决方案拆分到自己的库中, Aether .这完成了处理人工制品的所有繁重工作.通过Aether,您可以了解项目的依赖项,然后下载依赖项.通过Aether,您还可以在本地安装工件或将其部署到远程存储库.

以太不做的是管理一个项目.它不会清除目标目录或编译源.

我用Naether创建的只是访问Aether的一种简化方法.

I need to make a small program that downloads maven projects and prints its dependencies

something like this:

MavenArtifactRepository repository = new MavenArtifactRepository("typesafe", "http://repo.typesafe.com/typesafe/releases/", ..., ..., ...);
downloadAndPrintDependencies(repository, "org.hsqldb", "hsqldb", "2.2.9");

void downloadAndPrintDependencies(repository, groupId, artifactId, version) {
  MavenProject projectDescription = new MavenProject("org.hsqldb", "hsqldb", "2.2.9");
  Artifact artifact = repository.getProject(projectDescription);  // this would download the artificat in the local repository if necessary

  List<Dependency> dependecies = artifact.getDependencies();
  ...
}

and, that can execute goals on a maven project, something like this:

String pomXmlFile = "/tmp/myproject/pom.xml";
Reader reader = new FileReader(pomXmlFile);
MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
Model model = xpp3Reader.read(reader);

ProjectArtifact projectArtifact = new ProjectArtifact(model);
projectArtifact.clean();
projectArtifact.install();


any feedback on the pseudo-code?

What is the correct class and function that fetches an artifact from the repository?

what is the correct class and function that executes goals (such as clean and install) on maven projects?

解决方案

The Good

I have a project, Naether, that is a wrapper for Maven's dependency resolution lib Aether.

Using Naether, you can resolve Dependencies

import com.tobedevoured.naether.api.Naether;
import com.tobedevoured.naether.impl.NaetherImpl;

Naether naether = new NaetherImpl();
naether.addDependency( "ch.qos.logback:logback-classic:jar:0.9.29" );
naether.addDependency( "junit:junit:jar:4.8.2" );
naether.resolveDependencies();
System.out.println( naether.getDependenciesNotation().toString() );

Will output:

["ch.qos.logback:logback-core:jar:0.9.29",
 "ch.qos.logback:logback-classic:jar:0.9.29",
 "junit:junit:jar:4.8.2",
 "org.slf4j:slf4j-api:jar:1.6.1" ]

The Bad

I have no idea how to build (such as compile the source) a pom.xml via Java. I have searched around a little, but have not found a concrete example. A ProjectArtifact is just a artifact descriptor that Maven uses to resolve a POM, such as a parent POM. It does not expose build actions. Since there are a million ways to build a Maven project, there is no simple install method. You have to start the lifecycle of the install process somehow.

What Naether can do, build the project first and have Naether install it:

import com.tobedevoured.naether.api.Naether;
import com.tobedevoured.naether.impl.NaetherImpl;

Naether naether = new NaetherImpl();
naether.install( "com.example:sample:0.0.1", "/tmp/myproject/pom.xml", "/tmp/myproject/target/sample-0.0.1.jar" )


Update - How does it all fit together?

Building, deploy, installing, etc a Project is complicated. Maven does a pretty good job of simplifying it. Even though the Maven task is only install, there are numerous steps involved for that to work. For a simple Java project, that means populating the class path, compiling source, packaging the jar, and than installing it in the local Maven repository. Things only get more complicated when you talk about other ways to package a Java project, like a war.

The folk at Maven did the hard work and spun off the dependency resolution to its own library, Aether. This does all the heavy lifting of working with artifacts. Aether lets you figuring out what the dependencies are for a Project, download the dependencies. Aether also lets you install an artifact locally or deploy it to a remote repo.

What Aether does not do is manage a project. It does not clean up the target dir or compile source.

What I have created with Naether is just a simplified way to access Aether.

这篇关于以编程方式使用Maven的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆