如何正确打包maven多模块?

时间:2017-09-08 13:56:51

标签: java spring-boot maven-3

我正在使用Maven和Spring Boot构建一个多模块休息服务,该服务将在WebLogic Server上发布,我对如何配置应用程序模块的打包感到有些困惑。

我目前的结构是:

/应用
/ main(弹簧靴的主要方法)
/ api
/服务
/ dao

父项目(应用程序文件夹)设置为pom打包,但子项目我不知道如何配置它。我相信正确的方法是打包像.jar文件,但我在互联网上看到一些Java项目的例子,其中Maven多模块被定义为战争。

有人可以告诉我在这种情况下定义子模块包装的正确方法是什么?

我的父母pom:

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

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <javax.inject.version>1</javax.inject.version>
    </properties>

  <modules>
    <module>radarveicular-api</module>
    <module>radarveicular-service</module>
    <module>radarveicular-main</module>
    <module>radarveicular-dao</module>
  </modules>

  <build>

        <plugins>

            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                    <wtpversion>2.0</wtpversion>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

        </plugins>

    </build>

</project>

主要广告:

<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>com.radarveicular</groupId>
    <artifactId>radarveicular-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>radarveicular-main</artifactId>

  <dependencies>
      <dependency>
            <groupId>com.radarveicular</groupId>
            <artifactId>radarveicular-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- Spring Framework -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
  </dependencies>

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

</project>

api pom:

<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>com.radarveicular</groupId>
        <artifactId>radarveicular-application</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>radarveicular-api</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.radarveicular</groupId>
            <artifactId>radarveicular-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

服务pom:

<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>com.radarveicular</groupId>
        <artifactId>radarveicular-application</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>radarveicular-service</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.radarveicular</groupId>
            <artifactId>radarveicular-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
    </dependencies>

</project>

dao pom:

<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>com.radarveicular</groupId>
        <artifactId>radarveicular-application</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>radarveicular-dao</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>

</project>

2 个答案:

答案 0 :(得分:1)

我认为您的问题是您的父级pom.xml不应该处理您的春季启动项目。意思是,你应该把它包含在其中:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

我建议您的pom.xml父级仅将pom包装处理您的模块和依赖项:

<强>包装

<packaging>pom</packaging>

<强>模块

<modules>
    <module>radarveicular-api</module>
    <module>radarveicular-service</module>
    <module>radarveicular-main</module>
    <module>radarveicular-dao</module>
</modules>

然后,您的模块radarveicular-main将使用spring boot parent:

<强>父

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<强>包装

<packaging>war</packaging>

我希望这会对你有所帮助。 =)

答案 1 :(得分:1)

我觉得你的问题更多的是设计多模块的一部分。

应用程序的多模块管理可以依赖于各种事物。

  1. 如果您想将每个子模块部署为单独的模块/服务,可以根据需要关闭/打开。在这种情况下,如果您将每个模块构建为单独的war并单独部署它,它将对您有所帮助。

  2. 如果你必须将它作为单个战争部署而不是大多数人直接将所有依赖模块创建为jar并将它们包含在父项目中。通过这种方式,将部署一场战争。

  3. 您可以采用两种以上的混合方式。

  4. 希望我能正确理解你的问题。 :)

    快乐的编码!!