23

I am using Maven Assembly plugin to pack a jar file.

But when I run mvn package, maven always trigger the [jar:jar {execution: default-jar}] to create a default jar file.

So I will have 2 jar files (one created by Assembly plugin and one created by Maven jar which i don't want to be created).

How can I turn off the default-jar execution?

In my pom.xml, I am using: <packaging>jar</packaging>.

I don't want to change it to <packaging>pom</packaging>.

Lii
  • 11,553
  • 8
  • 64
  • 88
David
  • 3,538
  • 9
  • 39
  • 50

2 Answers2

40

(...) So i will have 2 jar files (one created by assembly plugin and one created by maven jar which i dont want to be created).

Looks like you're doing pretty complicated things. Maybe Maven is not the right tool in your case.

How can I turn off the execution: default-jar.

You can set the <phase> of the corresponding execution to something unknown, like none:

  <plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
      <execution>
        <id>default-jar</id>
        <phase>none</phase>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
        <!-- this is used for inheritance merges -->
        <phase>package</phase>
        <!-- append to the packaging phase. -->
        <goals>
          <goal>single</goal>
          <!-- goals == mojos -->
        </goals>
      </execution>
    </executions>
  </plugin>

This seems to work as long as you're providing something else to be installed, like an assembly (I only tested install). But of course, this is a hack.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 2
    It is a hack, but it's great to know this hack. Thanks! (+1) – Sean Patrick Floyd Nov 05 '10 at 09:08
  • BTW, do you ever sleep? You're on CET too, aren't you? – Sean Patrick Floyd Nov 05 '10 at 09:09
  • @seanizer: LOL. Yes I do :) And no, I'm currently not, I'm moving. – Pascal Thivent Nov 05 '10 at 12:22
  • I wouldn't consider this a hack. More of a way to turn off the 'default' plugins with maven when you don't need them. Beside, even the execution of the maven-jar-plugin adds time to the build even when there is nothing to build. So why even incur the time expense for something that does nothing? – jgifford25 Nov 05 '10 at 14:05
  • Thanks everybody. Special thanks to Pascal. I just start working with Maven 1 month ago. It is not easy for me. With your helping, now i feel better with Maven. – David Nov 05 '10 at 16:02
  • I know it has been a while, but using Maven 3.8.1 I get the error `Source must refer to an existing file, got [...]/target`. Obviously the target folder is empty. Can this hack be updated to current Maven? – Mathias Aug 22 '23 at 14:42
  • 1
    This answer is still correct and works nicely in current Maven versions, I tried with 3.9.4 just to be sure. I also use it in some projects. If you disagree, open a new question and post a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). Thank you. P.S.: If you add `my.package.Main`, the resulting JAR will even be executable. – kriegaex Aug 27 '23 at 06:29
  • I cannot verify my problem within a MWE, looks like there was a problem somewhere else in my POM. Sorry that I did not realise myself. – Mathias Aug 30 '23 at 09:51
4

While not a direct answer to the question, you could exclude the jar created by maven jar using <useProjectArtifact>false</useProjectArtifact>

Raghuram
  • 51,854
  • 11
  • 110
  • 122