Nick Dong
Nick Dong

Reputation: 3736

how to package jar file include classes in jar files

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?

Upvotes: 3

Views: 2239

Answers (3)

sendon1982
sendon1982

Reputation: 11234

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>

Upvotes: 2

sendon1982
sendon1982

Reputation: 11234

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.

Upvotes: 1

Keshava
Keshava

Reputation: 802

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.

Upvotes: 0

Related Questions