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

3.2k Views Asked by At

If all classes are up-to-date "Nothing to compile - all classes are up to date" so will maven create jar again?

As I am seeing in my log that jar is not creating again. so maven come to know that all classes are up-to-date.

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

1

There are 1 best solutions below

1
On BEST ANSWER

The Maven Jar Plugin will create a jar via its jar goal if none exists or skip its creation if existing but nothing changed.

You can force the creation of the jar via its forceCreation option (since version 2.2). From official documentation:

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.

Its default value is at false, which explains the behavior you are having.

If you want to force it always, you can add to your pom file:

<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>

Or just on a single build, invoke it as following:

mvn package -Djar.forceCreation=true

So, going back to your question:

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

The answer is: Yes, the Maven Jar Plugin works on this and the option above will change its behavior.