一尘不染

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

java

我在另一个构建工具的情况下使用Maven(leiningen为Clojure的,但是这不应该事),我想知道我会怎么称呼像一个插件的依赖:集结类路径编程(即通过Maven-
API,而不是通过mvn-command)。


阅读 219

收藏
2020-12-03

共1个答案

一尘不染

请参阅如何从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();
    }

}
2020-12-03