自定义的Maven插件:在一个构建中安装并执行该插件。

huangapple go评论64阅读模式
英文:

Custom maven plugin: install and execute the plugin in one build

问题

我编写了一个自定义的Maven插件,用于从自定义模式生成Java代码。

项目结构如下:

项目
+ 插件
+ Web应用程序

Reactor首先编译插件,然后是应用程序。

通常的mvn命令是:

mvn

... 触发的是 <defaultGoal>plugin:scaffold package</defaultGoal>

在全新的机器上,构建会失败,因为在反应堆规划构建阶段时插件尚未被识别。因此,我必须先调用 mvn install。然后 mvn plugin:scaffold package 可以正常工作。

问题是:每当我修改脚手架插件并调用 mvn plugin:scaffold package 时,脚手架插件的修改尚未生效,因为它尚未安装到存储库中。所以我必须再次先调用 mvn install

是否有一种方法可以:

  1. 安装对插件的修改
  2. 在一步中构建使用插件修改的Web应用程序?
英文:

I wrote a custom maven plugin that scaffolds java-code from a custom schema.

The project-structure is like this:

Project
+ plugin
+ web-application

The reactor compiles first the plugin, then the application.

The usual mvn-command is:

mvn

... who is triggering the &lt;defaultGoal&gt;plugin:scaffold package&lt;/defaultGoal&gt;

On fresh machines the build fails because the plugin is not yet known at the time the reactor plan the build-phases. So I have to call mvn install first. Then mvn plugin:scaffold package works like a charm.

The problem is: Whenever I modify the scaffolder-plugin and call mvn plugin:scaffold package the modifications of the scaffolder-plugin is not yet used because it is not yet installed into the repository. So I have to call mvn install first again.

Is there a way to:

  1. Install the modification to the plugin
  2. Build the webapplication using the modifications of the plugin

in one step?

答案1

得分: 5

以下是已经翻译的内容:

首先,你的插件必须是根项目的一个模块,以便正确进行解析:

<modules>
    <module>plugin</module>
    <module>app</module>
</modules>

然后在应用程序的 POM 文件中的 build/plugins 部分声明插件:

<build>
    <plugins>
        <plugin>
            <groupId>org.example.plugin</groupId>
            <artifactId>plugin</artifactId>
            <version>${project.parent.version}</version>
            <executions>
                <execution>
                    <id>sayhi</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>sayhi</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

第一次运行插件,或者当插件发生更改时,你需要至少运行 package 阶段,以便创建插件 JAR 文件。必须从根项目运行:

mvn package

插件将在 generate-sources 阶段执行:

[INFO] --- plugin:1.0-SNAPSHOT:sayhi (sayhi) @ app ---
[INFO] Hello, world.
[INFO]

当你更改插件时,只需再次运行(同样是从根项目):

mvn package

你将会看到变化:

[INFO] --- plugin:1.0-SNAPSHOT:sayhi (sayhi) @ app ---
[INFO] Hello, worldxxxx.
[INFO]

GitHub 上查看完整示例。

英文:

First your plugin must be a module of the root project for the resolution to work correctly:

&lt;modules&gt;
    &lt;module&gt;plugin&lt;/module&gt;
    &lt;module&gt;app&lt;/module&gt;
&lt;/modules&gt;

Then declare the plugin in the build/plugins section your application pom

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.example.plugin&lt;/groupId&gt;
            &lt;artifactId&gt;plugin&lt;/artifactId&gt;
            &lt;version&gt;${project.parent.version}&lt;/version&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;id&gt;sayhi&lt;/id&gt;
                    &lt;phase&gt;generate-sources&lt;/phase&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;sayhi&lt;/goal&gt;
                    &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

The first time you run the plugin or when the plugin changes you need to run at least the package phase so the plugin jar is created. It must be run from the root project:

mvn package

The plugin will be executed during the generate-sources phase:

[INFO] --- plugin:1.0-SNAPSHOT:sayhi (sayhi) @ app ---
[INFO] Hello, world.
[INFO] 

When you change the plugin just run (again from root project):

mvn package

and you will see the changes:

[INFO] --- plugin:1.0-SNAPSHOT:sayhi (sayhi) @ app ---
[INFO] Hello, worldxxxx.
[INFO] 

See a full example on Github

huangapple
  • 本文由 发表于 2020年9月8日 17:47:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63791337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定