Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an executable jar using maven?

My maven project is like this and I have a quartz.properties file in /src/main/resources folder as shown below

my-app
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |   `-- com
    |   |       `-- mycompany
    |   |           `-- app
    |   |               `-- App.java
    |   `-- resources
    |       `-- quartz.properties
    `-- test
        |-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

Now I want to make an executable jar using maven so that I can run it like this java -jar abc.jar. Below is my main method code which works fine in my laptop in my eclipse IDE but I want to run it on my ubuntu machine using java -jar command:

public static void main(String[] args) {
    StdSchedulerFactory factory = new StdSchedulerFactory();
    try {
        factory.initialize(App.class.getClassLoader().getResourceAsStream("quartz.properties"));
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
    } catch (SchedulerException ex) {
        System.out.println("error= " + ExceptionUtils.getStackTrace(ex));
    }
}

And here is my pom.xml file as of now. What changes I need to have in my pom.xml file to make an executable jar so that I can run it with java -jar?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.host.domain</groupId>
        <artifactId>DataPlatform</artifactId>
        <version>4.2.8-RELEASE</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>abc</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
    </dependencies>
</project>
like image 937
user1950349 Avatar asked Sep 14 '15 06:09

user1950349


People also ask

How do I create an executable jar from Maven?

In order to compile the project into an executable jar, please run Maven with mvn clean package command.


1 Answers

You can configure maven jar plugin to achieve this.

Try adding following just above dependencies section:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.mycompany.app.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

Here, <addClasspath>true</addClasspath> will add classpath entries to the manifest of your jar, so as long as you have those jars in same directory, your application will run fine.

You can customize the library path with, for example, <classpathPrefix>lib/</classpathPrefix>. It means your libraries should be placed in relative /lib directory.

You can also use maven dependency plugin if you want to automatically copy your libraries to output directory.

like image 65
Amila Avatar answered Oct 01 '22 09:10

Amila