1

I have a Maven project Foo, which uses maven-shade-plugin to package a webstart fat jar, one that includes all its dependencies.

<plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <finalName>foo-fat</finalName>
                ...

Another project is Bar, which is a web application that, among other things, distributes the Foo webstart. I need to copy foo-fat.jar into the Bar interim pre-package directory and for that, I am using maven-dependency-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>my-group</groupId>
                        <artifactId>foo</artifactId>
                        <outputDirectory>...</outputDirectory>                  
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

The problem is, this doesn't fetch foo-fat.jar but has Foo generate a simple jar foo.jar that doesn't include dependencies. It may be relevant to note that the only artifact Foo has in the local repo is the slim jar and I was unable to get the fat jar there by running any Maven lifecycle. Running mvn clean package in Foo directly does package a fat jar but not when called from Bar's pom.xml as is shown above.

How do I get maven-dependency-plugin to use a different project artifact, such as the fat jar made by the shade plugin?

amphibient
  • 29,770
  • 54
  • 146
  • 240

2 Answers2

3

For Foo class to be able to generate a fat jar when using maven install or maven package you will need to use maven-shade-plugin, which you are already doing but the key step there is to make sure to use a shadedClassifierName in its configuration. For example:

<artifactId>maven-shade-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <shadedClassifierName>shaded</shadedClassifierName>
        <shadedArtifactAttached>true</shadedArtifactAttached>
      </configuration>
    </execution>
  </executions>

Then in the Bar class in order to include the fat jar of Foo class you will need to use maven-dependency-plugin with copy goal and there make sure to annotate the artifactItem with shaded classifier

For Example:

<plugin>
  <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>copy</goal>
        </goals>
        <configuration>           
          <outputDirectory>${project.build.directory}/temp</outputDirectory>
          <artifactItems>
            <artifactItem>
              <groupId>...</groupId>
              <artifactId>...</artifactId>
              <version>${project.version}</version>
              <type>jar</type>
              <classifier>shaded</classifier>
            </artifactItem>
          </artifactItems>
        </configuration> 
      </execution>
    </executions>
</plugin>
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
aniketzr
  • 73
  • 1
  • 6
2

Re: "this [Bar][...] has Foo generate a simple jar foo.jar" – Are you sure that this is not from a previous build of Foo? Since dependency:copy just:

... copies a list of artifacts from the repository to defined locations

and re: "when [Foo] called from Bar's pom.xml as is shown above" – You can't "call", i.e. run a build of, a project from another one (but from an aggregator/multi-module project or with the Maven Invoker Plugin or a Groovy or Ant script or the like).


Now the idea for an answer (without having tried it):

Use install:install-file in Foo's install phase to make your foo-fat.jar a "real" artifact in your local repo and use this in Bar's <artifactItem> declaration.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • what is the difference between `install:install-file` and just `install`? I get an error doing install-file but not just install – amphibient Jul 11 '17 at 17:01
  • oh, i think i get it: install-file does individual artifact installation whereas install does everything -- please correct if wrong – amphibient Jul 11 '17 at 17:05
  • it works with the install. however, i wonder if there is any way for Bar to trigger that goal in Foo without doing it in Foo? not a big deal -- just a step elimination – amphibient Jul 11 '17 at 17:08
  • There are actually three items named `install`: 1) The phase of [Maven's default lifecycle](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference) as used in `mvn install`. 2) The shortcut for the _maven-install-plugin_: `install:...` 3) Its goal: `...:install`. The latter two referenced in my answer. You could use `maven-install-plugin:install` there, as well, if you like it verbose. But we devs are lazyeconomic typers, aren't we. ;) – Gerold Broser Jul 12 '17 at 22:00
  • [The `install` goal](https://maven.apache.org/plugins/maven-install-plugin/install-mojo.html) "_Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the local repository._". The [`install-file` goal](https://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html) "_Installs a file in the local repository._". That means an arbitrary artifact (`jar`, `war`, etc.) created somewhere outside of any Maven project. Note also that the former "_Requires a Maven project to be executed._". The latter runs independently of any project. – Gerold Broser Jul 12 '17 at 22:08
  • And while writing my previous comment it made me wonder why "_and any other artifacts attached by other plugins in the lifecycle_" apparently doesn't apply to the artifact created by the _maven-shade-plugin_ in your case. If I have some spare time I'm going to create a test project here and post the results. – Gerold Broser Jul 12 '17 at 22:15
  • See [this of my answers I gave recently](https://stackoverflow.com/a/45036037/1744774) regarding "_if there is any way for Bar to trigger that goal in Foo without doing it in Foo?_". – Gerold Broser Jul 12 '17 at 22:19