便捷的方法在Eclipse中更新多模块项目的版本。

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

Convenient way to update version of multi-module project in Eclipse

问题

我在Eclipse中有一个Maven多模块项目。

每次我想要更新版本,我都会转到“Run As -> Run Configurations...”,将目标“versions:set”的参数“newVersion”更改为较高的数字,然后运行该目标。

有没有更直接的方法?

请注意,我不能使用与Maven发布插件冲突的CI友好版本。

英文:

I have a Maven multi-module project in Eclipse.

Each time I want to update the version, I go to Run As -> Run Configurations..., change the parameter newVersion of the goal versions:set to a higher number and then run the goal.

Is there something more direct?

Note that I cannot use the CI-friendly versions because they clash with the Maven release plugin.

答案1

得分: 2

按照 https://stackoverflow.com/a/34091470/927493 的思路,

我将 newVersion 参数配置为 ${string_prompt:newVersion},如下图所示:

便捷的方法在Eclipse中更新多模块项目的版本。

然后我会得到要求输入版本号的提示,这正常运行。

英文:

Following the idea of https://stackoverflow.com/a/34091470/927493,

I configured the newVersion parameter to be ${string_prompt:newVersion} as shown in the image below

便捷的方法在Eclipse中更新多模块项目的版本。

Then I get prompted for the version number which works fine.

答案2

得分: 2

根据JF Meijer的答案,这是关于M2E的启动配置解释,我怀疑您已经安装了这个,它正好可以实现您所要求的功能。

启动创建对话框

  1. 打开“运行配置...”
  2. 选择“Maven Build”(Maven构建)
  3. 点击“新建启动配置”(位于左上角)
  4. 给它取个名字(例如“设置版本”)
  5. 点击“工作空间...”并选择父POM项目
  6. 在“目标”下输入“versions:set”
  7. 向下滚动(可能被隐藏)
  8. 在“参数”下,选择右侧的“添加...”
  9. 添加一个参数,具体如下:
  • 名称 = newVersion
  • 值 = ${string_prompt:"New Version":0.0.1-SNAPSHOT}

所以,为了进一步扩展JP Meijer已经指出的内容,如步骤9所示,变量string_prompt支持一个不同的名称,例如“New Version”,以及一个默认值,在这种情况下是0.0.1-SNAPSHOT。

现在,点击应用(Apply)、保存(Save)并运行(Run)!

英文:

Based on the answer from JF Meijer, here's a Launch Configuration explanation for M2E, which I suspect you already have installed, which does exactly what you're asking.

Launch Create Dialog

  1. Open "Run Configurations..."
  2. Select "Maven Build"
  3. Click "New Launch Configuration" (in the top left)
  4. Give it a name (Like "Set Version")
  5. Click "Workspace..." And select the parent pom project
  6. Enter "versions:set" under Goals
  7. scroll down (can be hidden)
  8. Under "Parameters" select Add... on the right
  9. Add a Parameter with
  • Name = newVersion
  • Value = ${string_prompt:"New Version":0.0.1-SNAPSHOT}

so, to expand on what JP Meijer already pointed out, the variable string_prompt, as shown under step 9, supports a distinct name, like "New Version", and a default value, in this case 0.0.1-SNAPSHOT.

Now, Apply, Save, and Run!

答案3

得分: 1

以下是检查Maven构建系统是否需要更新的脚本。通过运行此脚本,您将获得所有的更新(但不会应用这些更新)。这允许您根据需要更改所有、部分或不做更改。

mvn versions:display-plugin-updates
mvn versions:display-parent-updates
mvn versions:display-dependency-updates

通常我会将这个脚本保存在顶级目录下的 check-versions.shcheck-versions.cmd 文件中(根据脚本语言进行调整)。

要使这个脚本工作,您需要在 /project/build/plugins 目录下使用版本 2.7 或更高的 org.codehaus.mojo:versions-maven-plugin。我通常会像下面这样配置插件:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>versions-maven-plugin</artifactId>
  <version>2.7</version>
  <configuration>
    <generateBackupPoms>false</generateBackupPoms>
  </configuration>
</plugin>

这不会生成污染我的 git 历史的备份 pom.xml 文件。

在您第一次运行此脚本时,您可能会注意到继承的插件显示为过时的(因为它们实际上内置在 Maven 默认父 pom.xml 中的默认值中)。您将需要明确地将默认值定义为更新的版本,以停止它们的报告。

此外,您会发现许多插件仍然会报告,因为目前尚不清楚 Java 的最低所需版本和 Apache Maven 的最低所需版本。为了将这些要求编码到 pom.xml 中,您将使用 Maven Enforcer 插件。下面是一个强制使用 Maven 版本 3.5.4 和 Java 版本 11 的示例:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>3.0.0-M3</version>
  <executions>
    <execution>
      <id>enforce-maven</id>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <requireMavenVersion>
            <version>3.5.4</version>
          </requireMavenVersion>
          <requireJavaVersion>
            <version>11</version>
          </requireJavaVersion>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>

使用这些工具,我当前在我的一个项目中得到以下输出:

[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.edwbuck.parserhelp:pascal-adapter >----------------
[INFO] Building pascal-adapter 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.7:display-plugin-updates (default-cli) @ pascal-adapter ---
[INFO] artifact com.github.sevntu-checkstyle:dsm-maven-plugin: checking for updates from central
[INFO] artifact net.nicoulaj.maven.plugins:checksum-maven-plugin: checking for updates from central
[INFO]
[INFO] The following plugin updates are available:
[INFO]   maven-project-info-reports-plugin .................... 2.6 -> 3.0.0
[INFO]
[INFO] All plugins have a version specified.
[INFO]
[INFO] Project inherits minimum Maven version as: 3.5.4
[INFO] Plugins require minimum Maven version of: 3.2.1
[INFO]
[INFO] No plugins require a newer version of Maven than specified by the pom.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.976 s
[INFO] Finished at: 2020-04-16T07:52:12-05:00
[INFO] ------------------------------------------------------------------------
...

这表明我需要更新 maven-project-info-reports-plugin 以及 org.junit.jupiter:junit-jupiter-apiorg.junit.jupiter:junit-jupiter-engine 插件。

通常情况下,我不会使用 Maven 版本插件来实际在 pom.xml 文件中进行更新,因为文本编辑器速度很快,如果您想一次性在 pom.xml 中执行所有更新。

目前,Codehaus 提供的 Maven 版本插件没有命令行选项可以自动更新多个版本。原因很简单。要使用 versions:update-properties 插件,需要在 pom.xml 中定义更新策略(要更新什么/不更新什么)或在命令行上定义更新策略(要更新什么/不更新什么)。

这些策略非常冗长,因为它们涵盖整个项目。是的,它们可以使用通配符匹配,但仍然很冗长。对于我的个人项目,我注意到为了让它们能够正确地处理,我经常更新它们,所以我将它们从计划中剔除,而是在工作时根据 check-updates 脚本的输出来决定何时进行更新。

这是因为自动更新插件和依赖项并不总是安全的。有时候插件的下一个版本需要对项目进行代码更改。例如,从Java 8转移到Java 9的项目需要对构建和链接方式进行修改。对于依赖项也是如此,如果您希望将代码绑定到非废弃的API上。

英文:

Here is a script that checks if the maven build system needs updates. By running this script, you will get all the updates (but none of them will be applied). This lets you change all, some or none, as you see fit.

mvn versions:display-plugin-updates
mvn versions:display-parent-updates
mvn versions:display-dependency-updates

I typically save this script in a top-level check-versions.sh or check-versions.cmd (adjusting for the scripting language).

For this script to work, in /project/build/plugins you'll need the org.codehaus.mojo:versions-maven-plugin:2.7 or greater. I typically configure the plugin like so

&lt;plugin&gt;
  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
  &lt;artifactId&gt;versions-maven-plugin&lt;/artifactId&gt;
  &lt;version&gt;2.7&lt;/version&gt;
  &lt;configuration&gt;
    &lt;generateBackupPoms&gt;false&lt;/generateBackupPoms&gt;
  &lt;/configuration&gt;
&lt;/plugin&gt;

As this doesn't make backup pom.xml files which pollute my git history.

The first few times you run this, you might notice that the inherited plugins show themselves to be out of date (as they are effectively built-in to the defaults in the maven default parent pom.xml). You will have to explicitly define the defaults to a newer release to get them to stop reporting.

In addition, you will find that a lot of the plugins will still report because it isn't clear what is the minimum required version of Java and the minimum required version of Apache Maven. To encode these requirements into the pom.xml, you will use the Maven Enforcer plugin. An example of mine that forces Maven version 3.5.4 and Java version 11 is

&lt;plugin&gt;
  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt;
  &lt;version&gt;3.0.0-M3&lt;/version&gt;
    &lt;executions&gt;
      &lt;execution&gt;
        &lt;id&gt;enforce-maven&lt;/id&gt;
          &lt;goals&gt;
            &lt;goal&gt;enforce&lt;/goal&gt;
          &lt;/goals&gt;
          &lt;configuration&gt;
            &lt;rules&gt;
            &lt;requireMavenVersion&gt;
              &lt;version&gt;3.5.4&lt;/version&gt;
            &lt;/requireMavenVersion&gt;
            &lt;requireJavaVersion&gt;
              &lt;version&gt;11&lt;/version&gt;
            &lt;/requireJavaVersion&gt;
          &lt;/rules&gt;
        &lt;/configuration&gt;
      &lt;/execution&gt;
    &lt;/executions&gt;
  &lt;/plugin&gt;

With these tools in place, I currently get the output on one of my projects

[INFO] Scanning for projects...
[INFO]
[INFO] ---------------&lt; com.edwbuck.parserhelp:pascal-adapter &gt;----------------
[INFO] Building pascal-adapter 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.7:display-plugin-updates (default-cli) @ pascal-adapter ---
[INFO] artifact com.github.sevntu-checkstyle:dsm-maven-plugin: checking for updates from central
[INFO] artifact net.nicoulaj.maven.plugins:checksum-maven-plugin: checking for updates from central
[INFO]
[INFO] The following plugin updates are available:
[INFO]   maven-project-info-reports-plugin .................... 2.6 -&gt; 3.0.0
[INFO]
[INFO] All plugins have a version specified.
[INFO]
[INFO] Project inherits minimum Maven version as: 3.5.4
[INFO] Plugins require minimum Maven version of: 3.2.1
[INFO]
[INFO] No plugins require a newer version of Maven than specified by the pom.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.976 s
[INFO] Finished at: 2020-04-16T07:52:12-05:00
[INFO] ------------------------------------------------------------------------
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------&lt; com.edwbuck.parserhelp:pascal-adapter &gt;----------------
[INFO] Building pascal-adapter 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.7:display-parent-updates (default-cli) @ pascal-adapter ---
[INFO] Project does not have a parent.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.325 s
[INFO] Finished at: 2020-04-16T07:52:15-05:00
[INFO] ------------------------------------------------------------------------
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------&lt; com.edwbuck.parserhelp:pascal-adapter &gt;----------------
[INFO] Building pascal-adapter 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.7:display-dependency-updates (default-cli) @ pascal-adapter ---
[INFO] artifact com.edwbuck.parserhelp:pascal_client: checking for updates from central
[INFO] artifact org.junit.jupiter:junit-jupiter-engine: checking for updates from central
[INFO] artifact org.junit.jupiter:junit-jupiter-api: checking for updates from central
[INFO] artifact org.influxdb:influxdb-java: checking for updates from central
[INFO] The following dependencies in Dependencies have newer versions:
[INFO]   org.junit.jupiter:junit-jupiter-api ................... 5.6.0 -&gt; 5.6.2
[INFO]   org.junit.jupiter:junit-jupiter-engine ................ 5.6.0 -&gt; 5.6.2
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.848 s
[INFO] Finished at: 2020-04-16T07:52:18-05:00
[INFO] ------------------------------------------------------------------------

which indicates I need to update my maven-project-info-reports-plugin and my org.junit.jupiter:junit-jupiter-api and org.junit.jupiter:junit-jupiter-engine plugins.

Normally I don't use the maven versions plugin to actually do the update in the pom.xml file, because text editors are fast, if you're doing all the updates you want to do in the pom.xml at one time.

Currently the maven versions plugin offered by codehaus doesn't have an command line option to automatically update more than one version at a time. The reasons it is not there is simple. To use the versions:update-properties plugin, one needs to either:

  1. Define the update policy (what to update / what not to update) in the pom.xml.
  2. Define the update policy (what to update / what not to update) on the command line.

These policies are verbose as they cover the entire project. Yes, they can use glob matching, but they're still verbose. For my personal projects, I notice that for them to give me proper handling, I update them too often, so I leave them out of the picture, instead deciding what to update or not update at the time I work on the output of my check-updates script.

That's because it is not always safe to automatically update plugins and dependencies. Sometimes the next version of a plugin requires code changes to the project. For example, projects shifting from Java 8 to Java 9 require alterations to how they are built and linked. The same goes for dependencies, if you want to keep the code bound to non-deprecated APIs.

huangapple
  • 本文由 发表于 2020年4月10日 19:00:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61138914.html
匿名

发表评论

匿名网友

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

确定