-1

Is it possible to tell Maven, or one of its common plug-ins, to pack one of my dependency JARs within the final assembly as a JAR file?

ie If I depend on org.some-group:some-artifact:1.2.3, the Maven plug-in would just stuff the entire some-artifact-1.2.3.jar into my final JAR file?

0xbe5077ed
  • 4,565
  • 6
  • 35
  • 77
  • do you want to create independent jar file which includes all your third library jars as well? – Nitesh Virani Aug 08 '15 at 19:06
  • do you mean like `mvn install assembly:single` ? – Peter Lawrey Aug 08 '15 at 19:21
  • @NiteshVirani, I just have one designated dependency where I want to stuff its entire JAR file into my final assembled JAR file *without unpacking it*! I want all my other dependencies to be treated normally. – 0xbe5077ed Aug 08 '15 at 19:44
  • @PeterLawrey, I don't know if I mean that but I doubt it. Can you explain how `$ mvn install assembly:single` takes *one* designated dependency JAR and puts that JAR in the root directory of my final assembled JAR, while treating all of my other dependencies normally? – 0xbe5077ed Aug 08 '15 at 19:46
  • I am sorry, I have never heard of a program which needs just one of its depenencies but doesn't need any of the others. The simplest thing to is to copy them all, either as a single JAR or as a single directory. The problem with experienced IT people is they tend to think you are asking the wrong question if you are doing something very unusual. – Peter Lawrey Aug 08 '15 at 19:58
  • @PeterLawrey, I need to include the entirety of a signed JCE provider JAR file because I need to use it without messing with the digital signature. So I just pack that one JAR inside and then have my entry-point unpack it and drop it in the cwd. – 0xbe5077ed Aug 08 '15 at 20:03
  • I see you you don't want to merge your JAR(s) but rather add it as a JAR to your JAR. You can do this by adding the JAR as a resource. If you do this in two stages you can use one module to get the dependency you need and a second one to copy it to the resources folder so it get included. It's going to be something which is a bit of a hack. Note: you can run ant from maven via the plugin for ant, this allows you to script actions like this. – Peter Lawrey Aug 08 '15 at 20:08
  • Please read https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html to understand that the classes in the main jar are not able to access the files in those bundled jars. It is the bundled jar itself which becomes a resource on the classpath, not the content of the jar. – Robert Scholte Aug 08 '15 at 20:39
  • Hi @RobertScholte--I'm aware of this. As I mentioned to Peter Lawrey just above, I have my entry-point unpack the bundled JAR and drop it into the current working directory, where the classes it contains will then be accessible on the classpath. – 0xbe5077ed Aug 10 '15 at 17:09

1 Answers1

0

If I understood your question clearly, you need to add one specific jar into your generated jar, in this case you might use use classifier with maven-assembly-plugin

POM.xml

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>jar-with-dependencies-module</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <classifier>jar-with-dependencies</classifier>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <appendAssemblyId>false</appendAssemblyId>
          <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

  <id>final-assembly</id>
  <formats>
    <format>jar</format>
  </formats>

  <dependencySets>
    <!-- Include the jar-with-dependencies -->
    <dependencySet>
      <includes>
        <include>org.some-group:some-artifact:1.2.3</include>
      </includes>
      <useProjectArtifact>false</useProjectArtifact>
      <!-- Don't use transitive dependencies since they are already included in the jar -->
      <useTransitiveDependencies>false</useTransitiveDependencies>
    </dependencySet>t>
  </dependencySets>
</assembly>

Above configurations might give you an idea where to start and how you can include specific jars to your final jar

Nitesh Virani
  • 1,688
  • 4
  • 25
  • 41