How can I mvn:install shaded maven artifact

2.8k views Asked by At

This should have been easy but I'm experiencing strange behavior from maven-install plug-in.

I needed to repackage some common dependencies into my project to avoid dependency conflicts. for that purpose I used shade plugin with relocations configured:

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <id>do_shade</id>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <shadedArtifactAttached>false</shadedArtifactAttached>
                        <createSourcesJar>true</createSourcesJar>
                        <relocations>
                            <relocation>
                              <pattern>com.google.common</pattern>
                              <shadedPattern>com.myproject.google.common</shadedPattern>
                            </relocation>
                            <relocation>
                                <pattern>org.apache.commons</pattern>
                                <shadedPattern>com.myproject.commons</shadedPattern>
                            </relocation>
                        </relocations>
                    </configuration>
                </execution>
            </executions>
        </plugin>

The shade plugin did it's job correctly and produced shaded artifact com.myproject-myproject-.jar and dependecy-reduced pom file. But then install plugin installed the original artifact (without dependencies) instead of the shaded one.

Further more, before that install plugin problem my CI server (jenkins) built the project and correctly published the shaded artifact and dependency reduced pom to nexus repository (!!). So now it I download the artifact from nexus, i'll have correct jars in my local repository, but if I use install plugin, the jars won't be good.

Has anyone had similar issues? Does anyone know how to resolve them??

2

There are 2 answers

0
miljanm On BEST ANSWER

To answer my own question, the problem was that I later marked the dependencies-to-be-shaded as provided so that they don't get transitively pulled into my other projects. That caused shade plugin to not include them in shaded jar.

0
Dan Coates On

I also had this problem, where mvn install would build a shaded jar in my target directory but install the non-shaded one to my local Maven repository.

What ultimately proved to be the cause was that I had <finalName> defined in my maven-shade-plugin configuration. That ended up saving the shaded jar under that specific name, while the non-shaded jar took the default jar name which mvn install must look for when it comes time to install. Without <finalName> present, it copied the shaded jar appropriately into the local Maven repository.