英文:
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
。
是否有一种方法可以:
- 安装对插件的修改
- 在一步中构建使用插件修改的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 <defaultGoal>plugin:scaffold package</defaultGoal>
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:
- Install the modification to the plugin
- 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:
<modules>
<module>plugin</module>
<module>app</module>
</modules>
Then declare the plugin in the build/plugins
section your application pom
<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>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论