我是 Maven 的初学者,很多东西都不明白。我可以构建简单的可执行 jar,但如何将多模块 Maven 项目构建为可执行 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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Test</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Main</module>
<module>Dep</module>
</modules>
</project>
还有两个子项目:
<?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>
<artifactId>Test</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Main</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>Dep</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
and:
<?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>
<artifactId>Test</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Dep</artifactId>
</project>
主模块有带有 main 方法的 Main 类(笑)
public class Main {
public static void main(String[] args) {
Hello hello = new Hello();
System.out.println(hello.sayHello());
}
}
Hello 类在 Dep 模块中定义。我应该在我的 poms 中添加什么来构建可执行 jar,并使用 Main 模块中的 Main 类作为入口点?
您需要将 pom 更改为artifactid Main。
您需要添加 maven-assemble-plugin
在它的配置中,您可以选择在清单中指定 mainClass。这应该是主类的完全限定名称。
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>Main</artifactId>
<properties>
<!-- plugins -->
<maven.assembly.plugin.version>2.4</maven.assembly.plugin.version>
<!-- dependencies -->
<dep.version>1.0-SNAPSHOT</dep.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven.assembly.plugin.version}</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>Dep</artifactId>
<version>${dep.version}</version>
</dependency>
</dependencies>
</project>
在父项目上开始构建后,它应该在模块 Main 的目标子文件夹中创建可执行 jar。 (我得到的是一个名为 Main-1.0-SNAPSHOT-jar-with-dependency.jar 的 jar)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)