在Maven多模块项目中为一个模块创建jar文件

3
我正在开发一个基于Spring Boot和Maven的多模块电商应用。目录结构如下:
eshop
│   
├── eshop-core
│   └── pom.xml <--- eshop-core POM
├── eshop-admin
│   ├── EShopAdminApplication.java
|   └── pom.xml <--- eshop-admin POM
├── eshop-site
│   ├── EShopSiteApplication.java
|   └── pom.xml <--- eshop-site POM
└── pom.xml

eshop-siteeshop-admin模块依赖于eshop-core模块。

我正在使用STS IDE工作,在STS中将EShopSiteApplication.java作为Java应用程序运行时,该应用程序可以正常工作并且可以在本地访问。

我想创建一个JAR文件,当它被运行时,执行EShopSiteApplication.java。我尝试了STS导出功能和mvn clean install,但这些JAR文件无法工作。

以下是pom.xml文件:

eshop/pom.xml

<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>com.eshop</groupId>
  <artifactId>eshop</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>eshop</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.10.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>               
        </dependencies>
    </dependencyManagement>  
  <modules>
    <module>eshop-admin</module>
    <module>eshop-core</module>
    <module>eshop-site</module>
  </modules>
</project>

eshop-core/pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.eshop</groupId>
    <artifactId>eshop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../</relativePath>
  </parent>
  <artifactId>eshop-core</artifactId>
  <packaging>jar</packaging>
  <name>eshop-core</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>
  </dependencies>

  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

eshop-site/pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.eshop</groupId>
    <artifactId>eshop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../</relativePath>
  </parent>
  <artifactId>eshop-site</artifactId>
  <packaging>jar</packaging>
  <name>eshop-site</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
        <dependency>
                <groupId>com.eshop</groupId>
                <artifactId>eshop-core</artifactId>
                <version>${project.version}</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>

  </dependencies>

  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-assembly-plugin</artifactId>
                 <version>3.1.0</version>
                 <configuration>
                     <archive>
                         <manifest>
                             <mainClass>org.eshop.site.EShopSiteApplication</mainClass>
                         </manifest>
                     </archive>
                 </configuration>
             </plugin>
        </plugins>

    </build>
</project>
2个回答

0

可能您缺少与jar一起放置的依赖项。

请尝试

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <archive>
                <manifest>
                    <mainClass>
                        org.eshop.site.EShopSiteApplication
                    </mainClass>
                </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin> 

在对您的答案进行了一些更改后,它终于起作用了。我使用了 spring-boot-maven-plugin 并将目标从 single 更改为 repackage。谢谢! - R-R
<phase>package</phase><goals><goal>repackage</goal></goals> 对我很有用! - R-R

0

以下是我在这两个答案的帮助下成功实现的方法,答案1答案2

eshop-site/pom.xml

  <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <archive>
                            <manifest>
                                <mainClass>
                                    org.eshop.site.EShopSiteApplication
                                </mainClass>
                            </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
             </plugin>
        </plugins>

    </build>

eshop-site目录下,运行mvn clean install以构建项目,然后执行java -jar target\eshop-site-0.0.1-SNAPSHOT.jar来启动应用程序。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接