如何强制 Maven 总是创建一个新的 jar 文件?

How to force Maven to always create a new jar file?

如果所有 类 都是最新的“Nothing to compile - all classes are up to date” 那么maven会再次创建jar吗?

正如我在日志中看到的那样,jar 没有再次创建。所以 Maven 知道所有 类 都是最新的。

问题:是否有任何流程或其他东西可以解决这个问题?

如果 none 存在,则 Maven Jar Plugin will create a jar via its jar 目标;如果存在但没有任何改变,则跳过它的创建。

您可以强制通过其forceCreation option (since version 2.2). From official documentation创建jar:

Require the jar plugin to build a new JAR even if none of the contents appear to have changed. By default, this plugin looks to see if the output jar exists and inputs have not changed. If these conditions are true, the plugin skips creation of the jar. This does not work when other plugins, like the maven-shade-plugin, are configured to post-process the jar. This plugin can not detect the post-processing, and so leaves the post-processed jar in place. This can lead to failures when those plugins do not expect to find their own output as an input. Set this parameter to true to avoid these problems by forcing this plugin to recreate the jar every time.

其默认值为 false,这解释了您的行为。

如果你想强制总是,你可以在你的pom文件中添加:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <forceCreation>true</forceCreation>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>
  ...
</project>

或者仅在单个构建上,按如下方式调用它:

mvn package -Djar.forceCreation=true

所以,回到你的问题:

is there any process or another thing which work on this?

答案是:是的,Maven Jar 插件可以解决这个问题,上面的选项会改变它的行为。