How to programmatically call a Maven-task

前端 未结 4 1535
孤独总比滥情好
孤独总比滥情好 2021-02-05 08:34

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:bui

相关标签:
4条回答
  • 2021-02-05 08:54

    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>
    
    0 讨论(0)
  • 2021-02-05 09:02

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

    0 讨论(0)
  • 2021-02-05 09:05

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

    0 讨论(0)
  • 2021-02-05 09:15

    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();
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题