从Java代码生成Maven原型 [英] Generate Maven archetype from Java code

查看:59
本文介绍了从Java代码生成Maven原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以运行maven命令:archetype:generate用Java代码编写.我已经尝试过使用Maven嵌入程序,但是不建议使用该库.

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.

我要运行的maven命令例如:

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

对于某些原型,在执行此请求后,需要一些属性.我想在GUI屏幕上显示这些属性,就像m2eclipse插件一样,以便用户可以填写这些属性.

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.

有人有建议吗?

推荐答案

您可以尝试使用 Maven祈求者.

将此依赖项添加到 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 );
    }
}

这篇关于从Java代码生成Maven原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆