Generate Maven archetype from Java code

I want to know if it's possible to run the maven command: archetype:generate in Java code. I've tried this with the maven embedder, but this library is deprecated.

I want to do an archetype:generate from a remote catalog, and capture the required properties of the archetype.

The maven command I want to run is for example:

mvn archetype:generate \
    -DgroupId=com.maven \
    -DartifactId=test \
    -DarchetypeVersion=1.0-alpha-4 \
    -DarchetypeGroupId=org.apache.maven.archetypes \
    -DarchetypeArtifactId=maven-archetype-j2ee-simple \
    -DinteractiveMode=false \
    -DarchetypeCatalog=http://repo1.maven.org/maven2/archetype-catalog.xml

for some archetypes there are required properties, after you do this request. I want to display these properties on a GUI screen, just like the m2eclipse plugin does, so the user can fill in these properties.

Does anybody has a suggestion?

Answers


If all else fails, you can use Runtime.exec().


You could try to use the Maven Invoker.

Add this dependency to the pom.xml:

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

And here is what the code might look like:

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 );
    }
}

Need Your Help

mongoengine - dynamically generating Q()

python mongodb pymongo mongoengine

Is there a way to dynamically generate a Q() other than using eval().

java comparing integers as strings with a comparator - odd results

java int comparator dereference

I'm attemping to compare some players with a comparator by the amount of runs they have obtained.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.