Priyanka
Priyanka

Reputation: 75

run maven java project using batch file

I've a java application in which I'm using maven to load dependencies. My main method is in App.java and there are various other classes. This is a spring based application. I've to run this application using batch file.

This is what I've tried so far:

  1. made a manifest file to give main class name
  2. generated a jar of application
  3. in a lib folder placed all the jars which my app uses
  4. in manifest gave all the jars path

But I want to know if there is any other way I can achieve the same thing. Here in manifest I've to give all the jars names

Also in application jar, a manifest file is automatically created. So I've to manually edit it to give main class name and all dependent jars.

Any help appreciated.

Thanks.

Upvotes: 1

Views: 1669

Answers (1)

Ryan J
Ryan J

Reputation: 8323

Use the Maven Jar Plugin to do what you want. You can configure it to place all your manifest entries to meet your needs.

<plugin>
    <!-- jar plugin -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifestEntries>
                <Main-Class>package.path.for.App</Main-Class>
                <implementation-version>1.0</implementation-version>            
            </manifestEntries>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>  <!-- use this to specify a classpath prefix, in your case, lib -->
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
        </archive>
    </configuration>
</plugin>

To facilitate copying all your dependencies to a particular folder, use the Maven Dependency Plugin:

<plugin>
    <!-- copy all dependencies of your app to target folder-->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory> <!-- use this field to specify where all your dependencies go -->
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Upvotes: 6

Related Questions