Java – how to package and run a simple dependency command line application using Maven?

I'm new Java and maven, so it may be simple

If I follow Maven 2 Hello world's instructions:

http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Everything will do If you then modify POM XML to introduce a dependency from a remote repository, the file of the dependency is stored in ~ / M2 / repository / new dependency /

To run the application using the syntax in Hello world, you need to add the absolute path to the dependency of my classpath (by setting the environment variable or through the command line switch):

java -cp target/my-app-1.0-SNAPSHOT.jar:/.../.m2/.../new-dependency.jar com.mycompany.app.App

This will obviously clumsily:)

I suspect this is not the usual way to run Java programs, and I just need to read more about it Jar file, but when I do, I will appreciate any tips on how to execute correctly

I didn't use IDE, btw. VIM from the command line

thank you!

Microphone.

Solution

You can create a jar executable by adding the main - class attribute to its manifest file In maven, this is done by the archiver plug - in To add the main class attribute, add it to your POM In XML:

<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>

You can now run jar using the following command: Java - jar myjar Jar or double-click it (not available on all platforms)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>