javamavenmaven-jar-plugin

"Deleting" the generated default jar in Maven


There are solutions available to "disable" the default jar that is generated by the maven-jar-plugin. However, since my final artifacts probably depend on this jar (I am relatively new to maven, so not entirely sure if it is actually the case, but it seems so) when I disable it, my build fails with the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.3:shade (build-cli) on project putwb: Failed to create shaded artifact, project main artifact does not exist. -> [Help 1]

Otherwise, it produces an empty jar along with the other jars I am building through my scripts. Can somebody suggest a way to "delete" the default jar, after the build is complete? Alternatively, can someone point out how I can disable that yet my build succeeds?

I am posting the relevant part of my pom.xml below:

    <build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>readme-md</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.basedir}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <includes>
                                    <include>README.md</include>
                                </includes>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <!-- Build the CLI version -->
                    <phase>package</phase>
                    <id>build-cli</id>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <!-- Don't include the UI code -->
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>in/ac/iitk/cse/putwb/ui/**</excludes>
                            </filter>
                        </filters>
                        <finalName>putwb-cli-${project.version}</finalName>
                        <transformers>
                            <!-- Don't include images -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>.png</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>in.ac.iitk.cse.putwb.experiment.PUTExperiment</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
                <execution>
                    <!-- Build the UI version -->
                    <phase>package</phase>
                    <id>build-ui</id>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                    <finalName>putwb-ui-${project.version}</finalName>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>in.ac.iitk.cse.putwb.ui.PUTWb</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- If I add this to the script, the build fails -->
        <!--
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>default-jar</id>
                    <phase>none</phase>
                </execution>
            </executions>
        </plugin>
        -->
    </plugins>
  </build>

Solution

  • You can use ant-run plugin's delete task. You can execute this plugin in any phase that is later than package phase in the life cycle.(because it will be created again in package phase. Check which one suites you better.) Execute the maven command you choose as this plugins phase (like mvn verify if you choose verify phase)

            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-antrun-plugin</artifactId>
              <version>1.8</version>
              <executions>
                  <execution>
                      <phase>package</phase>
                      <goals>
                          <goal>run</goal>
                      </goals>
                      <configuration>
                          <target>
                              <delete file="${project.build.directory}/YourJarName.jar"/>
                          </target>
                      </configuration>
                  </execution>
              </executions>
        </plugin>
    

    Check Build life cycle https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

    thanks to @markthegrea for pointing the correct folder.