在pom中导入的依赖项中使用main()方法运行maven项目



有没有办法在 maven 项目的 pom 中导入依赖项。然后,当我们打包它并运行 jar 时,Main(( 方法由我们导入的依赖项提供,而不是在我们自己开发的项目中提供。

有没有可能做到?基本上,依赖项将从类路径加载我开发的项目,并消耗我实现的一些接口,它知道。

我还没有尝试过,但您可以使用 maven-jar-plugin 简单地在清单中指定主类,如下所示:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maventest</groupId>
<artifactId>aproject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>aproject</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.maventest.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

如果这不起作用,您可以随时提供自己的类,该类从所需的类导入和调用 main 方法。

你能试试这个吗:

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<dependencies>
<dependency>
<!--  Dependency which includes the main class -->
</dependency>
</dependencies>
<configuration>
<mainClass>absolute.name.MainClass</mainClass>
</configuration>
</plugin>
</plugins>

最新更新