21

I have a Maven project, but I am not familiar to Maven. I wanted to create an executable JAR file from this Maven project to use it in another project by eclipse. How can I do this?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Questioner
  • 662
  • 1
  • 10
  • 26
  • 1
    Follow this post: [http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven][1] [1]: http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven – Ankush soni Jul 31 '15 at 09:14

7 Answers7

33

To build jar From Eclipse, Right click on your maven project name then

Run as > Maven install

kswaughs
  • 2,967
  • 1
  • 17
  • 21
13

Right click maven project,

choose Run As-> Maven Build ....

Type package in the Goals box.

Click Run.

Priyantha
  • 4,839
  • 6
  • 26
  • 46
  • 2
    Yes, this helped me actually. Earlier I was trying the arguments `maven package` and `mvn package` which didn't work to me. Tried just `package` and it created a jar under the folder `target` with all supported maven jars into it. – Suresh Jan 31 '20 at 04:23
4

Command line approach:

In the root of the project (the maven project), should be a pom.xml. Go to that root and run mvn package. If this is correct, there should be a new folder with the name target in the root of the project. Inside this folder there should be the jar file.

Iker Aguayo
  • 3,980
  • 4
  • 37
  • 49
4

First of all, you have to remember about security in Java. Many jars would not work in fatjars, if they got included in other projects (for example bouncycastle).

If you are doing a simple executable jar that has no libs in it, and requires all of them on classpath, default build (when packageing tag is set to jar) would be ok, and just require a proper manifest.

If you need all libs inside (fatjar), you need to configure it yourself.

There are several plugins for it, for example maven-shade-plugin:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                            <exclude>META-INF/*.INF</exclude>
                        </excludes>
                    </filter>
                </filters>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>my.package.MainClass</Main-Class>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </transformer>
                </transformers>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>fat</shadedClassifierName>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
szefuf
  • 500
  • 3
  • 14
3

Add following into pom.xml file and Run as Maven Install. This worked for me.

pom.xml

<packaging>jar</packaging>

 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

<build>
    <plugins>

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.pohan.App</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>

    </plugins>


</build>

Now Run as Maven Install.

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
1

if you want with the terminal to do it type

 mvn package

and a line before "BUILD SUCCESS" the directory of the

enter image description here

Vladi
  • 1,662
  • 19
  • 30
0

Install maven - https://maven.apache.org/download.cgi

Goto your project in eclipse Run -> Maven install

jyotinadda
  • 51
  • 3