1

I am experiencing problems building a runnable jar (one that includes all of the dependencies I need). I can build one that includes all of the dependencies as jars and it works but is very slow (I built it using Eclipse Export). I tried using the maven-assembly-plugin.

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>${mainClass}</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id> 
                <phase>package</phase> 
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

This creates a Jar with all of the dependencies un-packaged. The problem is that the resultant jar throws an error when I run it (I get a null pointer exception in my code which appears to be cause by a factory class in one of the included libraries returning NULL).

The problem appears to be that when one of the dependent jars is un-packaged for inclusion into my jar something is going wrong (hence the error). I want to leave that jar packaged but still have it within my runnable jar.

Is there a way to selectively include the Jar packaged (or for that matter include all the jars packaged)?

Richard B
  • 895
  • 13
  • 39
  • Or probably you could exclude the unwanted jars? https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/including-and-excluding-artifacts.html – UserASR Jan 22 '20 at 12:18
  • You want to exclude one of the dependant jars while packaging(all dependency package), but don't want to exclude it from pom? – Ashutosh Sharma Jan 22 '20 at 12:18
  • Selective inclusion can also be done : https://maven.apache.org/plugins/maven-assembly-plugin/assembly-component.html#class_dependencySet – UserASR Jan 22 '20 at 12:20
  • The problem appears to be that when one of the dependent jars is un-packaged for inclusion into my jar something is going wrong (hence the error). I want to leave that jar packaged but still have it within my runnable jar – Richard B Jan 22 '20 at 12:26

1 Answers1

0

I don't know, if i understand you right.

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
            </plugin>

            <plugin>
                <groupId>com.jolira</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <version>1.4.4</version>
                <executions>
                    <execution>
                        <configuration>
                            <mainClass>${mainclass}</mainClass>
                            <attachToBuild>true</attachToBuild>
                            <filename>${project.artifactId}-${project.version}-jar-with-dependencies.${project.packaging}</filename>
                        </configuration>
                        <goals>
                            <goal>one-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
  • you can exclude jars in the dependency-section
        <dependency>
            <groupId>...</groupId>
            <artifactId>...</artifactId>
            <version>...</version>
            <exclusions>
                <exclusion>
                    <groupId>...</groupId>
                    <artifactId>...</artifactId>
                </exclusion>
            </exclusions>
Wombattle
  • 103
  • 2
  • 8
  • I avoided one-jar as it doesn't seem to have been supported since 2012. Am I wrong in that observation? – Richard B Jan 22 '20 at 12:29
  • Yes, you are right. An alternative (not really) is the usage of the assembly-plugin with an descriptor (see above, first link). But this needs something to load the classes\jars because the ```Class-Path``` doesn't point to jars within a jar. – Wombattle Jan 22 '20 at 12:47