Alex Shesterov
Alex Shesterov

Reputation: 27525

Maven JAR Plugin 3.0.2 Error: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them

Maven JAR plugin (version 3.0.2) keeps throwing the following error, even for a single invocation of the jar goal:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:3.0.2:jar (default) on project test: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them. -> [Help 1]

Here's a (minimal?) pom.xml which demonstrates the problem:

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>

  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <executions>
          <execution>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

The invocation is just mvn package.

According to the documentation, classifier only needs to be specified on multiple invocations of the same goal, and there's a reasonable default for the test-jar goal which I don't intend to change.

Also, the problem doesn't seem to appear on the 2.x line of the JAR plugin.

Did I miss something? Could anybody please suggest what I am doing wrong?

P.S. The Maven version is 3.3.9.

Upvotes: 57

Views: 71358

Answers (7)

Craigo
Craigo

Reputation: 3727

I'm using the maven-assembly-plugin and when I upgraded from Maven 3.6.3 to Maven 3.9.6 I started getting this error, even though I never added the maven-jar-plugin. The solution was to add it as disabled like this:

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>default-jar</id>
            <phase>none</phase>
            <configuration>
                <finalName>unwanted</finalName>
                <classifier>unwanted</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>

Upvotes: 0

DAB
DAB

Reputation: 1873

I got the error

Maven JAR Plugin 3.0.2 Error: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them

when I was building a WAR but needed an extra JAR file to resolve external dependencies in another project.

I solved the problem simply by adding a configuration element containing a "classifier". This simply means the JAR file will have the classifier as a suffix in its name.

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <executions>
            <execution>
              <phase>install</phase>
              <goals>
                <goal>jar</goal>
              </goals>
              <configuration>
                  <classifier>identify_your_jar_file_differently</classifier>
              </configuration>
            </execution>
          </executions>
        </plugin>           

Upvotes: 6

Doua Beri
Doua Beri

Reputation: 10949

A little late but you should add <phase>none</phase> to default-jar execution. this will skip it.

<execution>
  <id>default-jar</id>
  <phase>none</phase>
</execution>

This will skip the default-jar but others are executed. Make sure you put <phase>package</phase> in the rest of the execution threads.

Upvotes: 2

Ondra Žižka
Ondra Žižka

Reputation: 46816

One reason to have this error may be that the package goal is mistakenly executed twice:

mvn ... package ... package

May happen when the command is built by imperfect scripts :)

Upvotes: 1

Derik
Derik

Reputation: 159

In my case, I have set the execution ID to default-jar, overriding the default execution. Then the error is gone.

<execution>
    <id>default-jar</id>
    <phase>package</phase>
    <goals>
        <goal>jar</goal>
    </goals>
</execution>

Upvotes: 15

Wolfgang Fahl
Wolfgang Fahl

Reputation: 15769

If your logs shows something like:

[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ test --
[WARNING] JAR will be empty - no content was marked for inclusion!

Adding a single useless class in src/main/java seems to solve the issue see:

http://maven.40175.n5.nabble.com/quot-mvn-clean-verify-deploy-quot-causes-jar-plugin-to-execute-twice-td5877166.html

Since the above link might be broken as of 2021-09 you might want to try http://mail-archives.apache.org/mod_mbox/maven-users/201608.mbox/thread as an alternative

Upvotes: 10

Tunaki
Tunaki

Reputation: 137104

The Jar Plugin is actually getting executed twice with the configuration:

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.2</version>
  <executions>
    <execution>
      <goals>
        <goal>jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

If you check the logs with such a configuration, you will have something like:

[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ test ---
[INFO] Building jar: ...\test\target\test-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default) @ test ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

meaning that the plugin was in fact executed twice. What happens, is that the Jar Plugin, in a project that has a packaging of jar has a default execution bound to the package phase. This default execution is the one mentioned in the logs with the ID of default-jar.

When you configured an <execution> in the plugin, you actually configured a new execution, where the jar goal of the plugin is to be invoked. Since the jar goal binds by default to the package phase, that execution is getting executed at that phase, after the default binding inherent to the jar packaging. And since the plugin ran already, it is failing because running it again would actually replace the main artifact already produced during the first run. This error was added in version 3.0.0 of the plugin in MJAR-198, because such a thing happening is very likely a mis-configuration which should be detected early.

As such, the fix is simple: don't have an execution that specifies the goal jar, and let the default one (coming from the jar packaging) do the work. The JAR will still be created, even without the explicit configuration of the jar goal, thanks to the default execution. If you want a test JAR as well, you will still need to configure the plugin to do that with:

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.2</version>
  <executions>
    <execution>
      <goals>
        <goal>test-jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

But note that the goal jar is not specified.

Upvotes: 81

Related Questions