您可以尝试使用Maven Invoker。
将此依赖项添加到pom.xml
:
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>2.1.1</version>
</dependency>
下面是代码的样子:
import org.apache.maven.shared.invoker.*;
import java.util.Collections;
import java.util.Properties;
public class MavenInvoker {
public static void main(String[] args) throws MavenInvocationException {
InvocationRequest request = new DefaultInvocationRequest();
request.setGoals( Collections.singletonList("archetype:generate") );
request.setInteractive(false);
Properties properties = new Properties();
properties.setProperty("groupId", "com.maven");
properties.setProperty("artifactId", "test");
properties.setProperty("archetypeVersion", "1.0-alpha-4");
properties.setProperty("archetypeGroupId", "org.apache.maven.archetypes");
properties.setProperty("archetypeArtifactId", "maven-archetype-j2ee-simple");
properties.setProperty("archetypeCatalog", "http://repo1.maven.org/maven2/archetype-catalog.xml");
request.setProperties(properties);
Invoker invoker = new DefaultInvoker();
InvocationResult result = invoker.execute( request );
}
}