Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a Netbeans style Jar with all dependencies in a lib folder?

As the question says, how to package a Netbeans Maven project exactly the way a Netbeans native project is packaged:

  • All the dependencies in a separate lib folder
  • The main project jar with a manifest that includes the lib folder on it's classpath
like image 324
The Coordinator Avatar asked Jul 15 '13 12:07

The Coordinator


People also ask

Does a jar file contain all dependencies?

the executable jar should NOT contain the dependencies, just enough of your own code to run, with the dependent classes coming from the before mentioned classpath set up in the manifest.


1 Answers

In your pom.xml file ...

1) Add this code to your project->properties node. This will define your main class in a central place for use in many plugins.

<properties>
        <mainClass>project.Main.class</mainClass>
</properties>

2) Add this code to your project->build->plugins node. It will collect all your jar dependencies into a lib folder AND compile your main class jar with the proper classpath reference:

    <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <phase>install</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>${mainClass}</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
like image 157
The Coordinator Avatar answered Jan 20 '23 05:01

The Coordinator