如何以编程方式调用 Maven 任务

发布于 2024-08-18 23:32:08 字数 261 浏览 10 评论 0原文

我在另一个构建工具(Clojure 的 leiningen,但这不重要)的上下文中使用 Maven,我想知道如何调用像 dependency:build-classpath 以编程方式(即通过 Maven-API,而不是通过 mvn-命令)。

I'm using Maven in the context of another build-tool (leiningen for Clojure, but this should not matter), and I would like to know how I would call a plugin like dependency:build-classpath programmatically (i.e. via the Maven-API, not via the mvn-command).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烂人 2024-08-25 23:32:08

使用 Maven Invoker API。它又快又容易。

InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile( new File( "/path/to/pom.xml" ) );
request.setGoals( Collections.singletonList( "install" ) );

Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File("/usr"));

try
{
  invoker.execute( request );
}
catch (MavenInvocationException e)
{
  e.printStackTrace();
}

pom.xml:

<dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>maven-invoker</artifactId>
    <version>2.1.1</version>
</dependency>

Use the Maven Invoker API. Its quick and easy.

InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile( new File( "/path/to/pom.xml" ) );
request.setGoals( Collections.singletonList( "install" ) );

Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File("/usr"));

try
{
  invoker.execute( request );
}
catch (MavenInvocationException e)
{
  e.printStackTrace();
}

pom.xml:

<dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>maven-invoker</artifactId>
    <version>2.1.1</version>
</dependency>
你的他你的她 2024-08-25 23:32:08

这是一个更好的解决方案:使用 Mojo-Executor 库。它公开了一个简单的 Java API 用于调用 Maven 插件。

Here's a better solution: use the Mojo-Executor library. It exposes a simple Java API for invoking Maven plugins.

哭了丶谁疼 2024-08-25 23:32:08

了解 maven-plugin-testing-harness 中的 org.apache.maven.plugin.testing.AbstractMojoTestCase 是如何实现的。这是我的一些测试中的代码片段,可能会有所帮助。

public abstract class JAXBGenerateTest extends AbstractMojoTestCase {

    static {
        System.setProperty("basedir", getBaseDir().getAbsolutePath());
    }

    protected MavenProjectBuilder mavenProjectBuilder;

    protected void setUp() throws Exception {
        super.setUp();

        mavenProjectBuilder = (MavenProjectBuilder) getContainer().lookup(
                MavenProjectBuilder.ROLE);
    }

    protected static File getBaseDir() {...}

    /**
     * Validate the generation of a java files from purchaseorder.xsd.
     * 
     * @throws MojoExecutionException
     */
    public void testExecute() throws Exception {

        final File pom = new File(getBaseDir(),
        "src/test/resources/test-pom.xml");

        final ArtifactRepository localRepository = new DefaultArtifactRepository( "local", 

                new File(getBaseDir(), "target/test-repository").toURI().toURL().toString()             , new DefaultRepositoryLayout());


        final MavenProject mavenProject = mavenProjectBuilder.build(pom, localRepository, null);


        final XJC2Mojo generator = (XJC2Mojo) lookupMojo("generate", pom);
        generator.setProject(mavenProject);
        generator.setLocalRepository(localRepository);
        generator.setSchemaDirectory(new File(getBaseDir(),"src/test/resources/"));
        generator.setSchemaIncludes(new String[] { "*.xsd" });
        generator.setBindingIncludes(new String[] { "*.xjb" });
        generator.setGenerateDirectory(new File(getBaseDir(), "target/test/generated-sources"));
        generator.setVerbose(true);
        generator.setGeneratePackage("unittest");
        generator.setRemoveOldOutput(false);

        generator.execute();
    }

}

See how org.apache.maven.plugin.testing.AbstractMojoTestCase from maven-plugin-testing-harness is implemented. Here's a code snippet from some of my tests which may be helpful.

public abstract class JAXBGenerateTest extends AbstractMojoTestCase {

    static {
        System.setProperty("basedir", getBaseDir().getAbsolutePath());
    }

    protected MavenProjectBuilder mavenProjectBuilder;

    protected void setUp() throws Exception {
        super.setUp();

        mavenProjectBuilder = (MavenProjectBuilder) getContainer().lookup(
                MavenProjectBuilder.ROLE);
    }

    protected static File getBaseDir() {...}

    /**
     * Validate the generation of a java files from purchaseorder.xsd.
     * 
     * @throws MojoExecutionException
     */
    public void testExecute() throws Exception {

        final File pom = new File(getBaseDir(),
        "src/test/resources/test-pom.xml");

        final ArtifactRepository localRepository = new DefaultArtifactRepository( "local", 

                new File(getBaseDir(), "target/test-repository").toURI().toURL().toString()             , new DefaultRepositoryLayout());


        final MavenProject mavenProject = mavenProjectBuilder.build(pom, localRepository, null);


        final XJC2Mojo generator = (XJC2Mojo) lookupMojo("generate", pom);
        generator.setProject(mavenProject);
        generator.setLocalRepository(localRepository);
        generator.setSchemaDirectory(new File(getBaseDir(),"src/test/resources/"));
        generator.setSchemaIncludes(new String[] { "*.xsd" });
        generator.setBindingIncludes(new String[] { "*.xjb" });
        generator.setGenerateDirectory(new File(getBaseDir(), "target/test/generated-sources"));
        generator.setVerbose(true);
        generator.setGeneratePackage("unittest");
        generator.setRemoveOldOutput(false);

        generator.execute();
    }

}
尘曦 2024-08-25 23:32:08

Ant Maven 任务 可以让您了解如何执行此操作(DependencyTask 源)。另一条路线可能是 PomModuleDescriptorParserIvy 使用。

The Ant Maven tasks can give you an idea how to do it (DependenciesTask source). Another route may be the PomModuleDescriptorParser used by Ivy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文