1

Good afternoon, I want to use the Maven Shade plugin to package the jar-file. But each build creates 2 jar-files.

Why? And how to make 1 jar-file packed?

my pom.xml :

<groupId>test</groupId>
<artifactId>task2_maven</artifactId>
<version>1.0</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>test.run.Runner</Main-Class>
                                    <Build-Number>1</Build-Number>
                                </manifestEntries>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>

my output:

enter image description here

Please help and let me know what am doing wrong.

Ilya Y
  • 744
  • 6
  • 24
  • @Stewart no, i have another question – Ilya Y Dec 18 '19 at 11:50
  • 1
    I think (from memory) the jar prefixed with original is the jar without libraries, i.e your code as would be normally compiled/jared, the second jar is the jar with libraries, this is normal behaviour default behaviour. – Gavin Dec 18 '19 at 11:53
  • @Gavin i know. But can i able to create one file? maybe there is a special configuration? – Ilya Y Dec 18 '19 at 11:55
  • 1
    As @jirka,pinkas says in his answer the first is compiled by the maven (by the compile or jar plugin i think), the second by shade, your best bet is to look at the configuration for mavens compile/jar pulgin, but to be honest I would just ignore it as a transitive build artefact – Gavin Dec 18 '19 at 12:00
  • @Gavin And is it possible after the Assembly to automatically delete an unnecessary file to leave one? – Ilya Y Dec 18 '19 at 12:02
  • 1
    I dont see why not. So long as you do the delete as the final action it shouldnt cause any problem, though I dont know which plugin you would use. – Gavin Dec 18 '19 at 12:04
  • 1
    you could delete it using maven-antrun-plugin: https://stackoverflow.com/questions/18637626/remove-or-delete-resource-files-from-target-directory-using-pom-file just set it so that it will run after package phase. – jirka.pinkas Dec 18 '19 at 12:06
  • @jirka.pinkas Excuse me please. How to make a vase plugin "after package"? – Ilya Y Dec 18 '19 at 12:17
  • @Axel23 This one then https://stackoverflow.com/questions/4088701/maven-shaded-jar-is-prefixed-with-original-in-the-file-name/8696195 – Stewart Dec 18 '19 at 13:23

1 Answers1

2

Why does this bother you?

  1. During package phase, Maven will create target/artifact-version.jar
  2. Shade plugin will run and it will rename that jar to target/original-artifact-version.jar and give the shaded JAR the name target/artifact-version.jar

So just ignore target/original-artifact-version.jar which just contains result of (1.)

Edit: Add this plugin and run mvn clean install But as I was trying to say, I don't see a point in deleting the file

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <delete>
                                <fileset dir="${project.build.directory}" includes="original-*.jar" />
                            </delete>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
jirka.pinkas
  • 918
  • 6
  • 21
  • @jirka-pinkas that is, creating a single jar file is not possible? It's on my study assignment. In ANT I could put everything in one jar file. I thought. which maven can do nicely, too. – Ilya Y Dec 18 '19 at 11:58
  • 1
    All your class files and dependencies are in single JAR - in your case task2_maven-1.0.jar. You don't need to worry about original* JAR file at all, just ignore it. – jirka.pinkas Dec 18 '19 at 12:02
  • 1
    Added fully working example configuration. Note: Use last edit, I changed tag "tasks" tag to "target", because "tasks" was deprecated. – jirka.pinkas Dec 18 '19 at 12:32