3

My project have some dependcies which are jar files. My project will be packaged as a jar file.

Is it possible to package all files including classes compiled from sources of my project and all classes under depended jar files. I am using JDK 1.6+.

If it is possible, how to do that using command or ant or Eclipse?

Nick Dong
  • 3,638
  • 8
  • 47
  • 84

3 Answers3

2

Copy dependency jar class files into the project jar file:

Use Maven plugin maven-shade-plugin by command: mvn package

All the class files under <dependencies> will be included in your project jar file.

<dependencies>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.5</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <finalName>Test.jar</finalName>
            </configuration>
        </plugin>
    </plugins>
</build>
sendon1982
  • 9,982
  • 61
  • 44
1

What you asked is possible. but not a good way to do that. Jar files are like components and should stay in their own jar files. If you include all the class files into one jar, it is impossible to reuse them and your jar file will be too large.

I recommend you use Maven (http://maven.apache.org), which can manage your dependency for you.

sendon1982
  • 9,982
  • 61
  • 44
0

Use Export option in eclipse. Export->Runnable Jar and select the option "Package the required libraries into generated jar"

This should fit your needs.

HTH, Keshava.

Keshava
  • 702
  • 1
  • 7
  • 20
  • Please refer another post here http://stackoverflow.com/questions/8828493/how-to-do-eclipses-export-jar-from-the-command-line Also see ANT example http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html . – Keshava Jul 28 '14 at 11:14
  • That would be using jar xvf to unpack other packages and then using mv to move their classes to the target folder and then using jar cvf to create the target jar. Is it the elegant way? Is there any alternative ways? @Keshava – Nick Dong Jul 30 '14 at 02:13
  • For eclipse, Is that can do same thing when building project automatically not export manually? – Nick Dong Jul 30 '14 at 02:15