且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用maven打包和运行具有依赖项的简单命令行应用程序?

更新时间:2022-01-27 03:31:20

您可以通过将Main-Class属性添加到其清单文件来使jar成为可执行文件.在Maven中,这是由存档程序插件完成的.要添加Main-Class属性,请将其添加到pom.xml:

You can make a jar executable by adding the Main-Class attribute to its manifest file. In Maven this is done by the archiver plugin. To add the Main-Class attribute, add this to your pom.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>

您现在可以使用以下命令运行jar:java -jar myjar.jar或双击它(并非在所有平台上都可用).

You can now run your jar with the command: java -jar myjar.jar or by double clicking on it (not available in all platforms).