4

我正在创建一个 Maven 原型,并且正在查看其他原型的来源。

我能够弄清楚文件和文件夹是如何创建的,以及在原型文件中是如何发生替换的

现在我想编写一些在原型为新项目运行时运行的代码,以操作复制的文件并做一些其他事情。

我怎么做 ?制作原型的指南似乎已经过时,并且从未提及过。

4

2 回答 2

2

我使用原型 Maven 插件的属性 -Dgoal。在此属性中,您可以指定其他目标以立即在从原型创建的项目上运行。

因此,我在我的 maven 原型项目中创建了一个新的 maven 插件模块,该模块包含我想要执行的所有其他 java 逻辑。我不需要在生成的项目的新创建的 pom.xml 中指定这个插件。

更多细节:这里是 maven-plugin 的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>yourGroupId</groupId>
<artifactId>init-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>   

<dependencies>       
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-project</artifactId>
        <version>3.0-alpha-2</version>
    </dependency>
</dependencies>

这是魔咒:

/**
 * @goal maven-plugin-init
 */
public class InitMojo extends AbstractMojo
{    
    /**
     * @parameter expression="${project.basedir}"
     */
    private File basedir;
    public void execute() throws MojoExecutionException, MojoFailureException
    {
       //write initialization logic here
    }
}

因此,在“basedir”中将是新创建项目的 basedir。

唯一的问题是如何将原型的输入参数传递给我们的初始化插件。我只是在新创建的项目的根目录中使用原型创建一个文件“initial.properties”,并将所有输入参数存储在那里。然后在初始化插件中读取这个文件。

要启动您的原型,请使用以下命令:mvn archetype:generate $archetype_properties -Dgoals=yourGroupId:init-maven-plugin:maven-plugin-init

于 2013-05-24T09:19:34.453 回答
0

V. Artyukhov 是对的,但是你想做的事情(修改文件等)可以很容易地通过使用 -Dgoals:antrun:run 和下面的 pom 来完成。您不需要自己的插件。Ant 在这里很有意义,因为您可以将许多文件操作放在一个语句中。请注意使用命令行时执行的 id default-cli (如 -Dgoals 所做的那样)。

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>default-cli</id>
            <configuration>
                <target>
                    <ant antfile="src/ant/custom.xml" inheritRefs="true">
                        <property name="customGroupId" value="${customGroupId}"/>
                        <property name="customArtifactId" value="${customArtifactId}"/>
                        <property name="customVersion" value="${customVersion}"/>
                    </ant>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
于 2012-03-15T13:24:06.960 回答