1
votes

is it possible to build a jar file made up of all jar files that I need? I am building my project and the jar files are not being included in the generated jar file and I would like to generate one separate jar file which contains all jar files in this project

5
take a look at this post: stackoverflow.com/questions/183292/…dexametason
"is it possible to build a jar file made up of all jar files that I need?" It is generally better to include a manifest in the main Jar which identifies the dependent Jars by relative paths. Why do you not implement that strategy?Andrew Thompson

5 Answers

3
votes

There are Eclipse plugins that can do that FatJar, JarsSplice

0
votes

The yguard tool can do this. It can also shrink your code and obfuscate it if you wish.

Here is an ant example from their documentation:

  <taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" classpath="buildlib/yguard.jar"/>

  <inoutpairs resources="auto">
    <fileset dir="${input-lib-dir}">
      <include name="myapp*.jar"/>
      <exclude name="*_obf.jar"/>
    </fileset>
    <mapper type="glob" from="*.jar" to="*_obf.jar"/>
  </inoutpairs>
0
votes

Not possible. You can't include jars in a jar in order to have them on the classpath (you can put them in the jar, but you can't use them as dependencies)

Maven, however, gives you an alternative with the maven-assembly-plugin. It unpacks all jars and stores their .class files in your new, single jar. There are, of course, other options to do the same thing, but maven is a great build tool to use anyway.

0
votes

You can use one jar maven plug in.

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!--                   REPOSITORIES                          -->
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

    <pluginRepositories>
        <pluginRepository>
            <id>onejar-maven-plugin.googlecode.com</id>
            <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
        </pluginRepository>
    </pluginRepositories>
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
            <!--                      ONE JAR PLUGIN                     -->
            <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.acme.MainClass</mainClass>
                            </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.dstovall</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <version>1.4.4</version>
                <executions>
                    <execution>
                        <configuration>
                            <!-- Optional -->
                            <onejarVersion>0.97</onejarVersion>
                            <!--
                                Optional, use only if you need to include native libraries
                                (dll's) <binlibs> <fileSet>
                                <directory>${project.build.directory}/dllextract</directory>
                                <includes> <include>test.dll</include> </includes> </fileSet>
                                </binlibs>
                            -->
                            <!-- Optional, default is false -->
                            <attachToBuild>true</attachToBuild>
                            <!-- Optional, default is "onejar" -->
                            <classifier>onejar</classifier>
                        </configuration>
                        <goals>
                            <goal>one-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>