将多个弹簧Web项目合并为一个可部署的战争

时间:2017-03-15 06:57:04

标签: maven spring-mvc war pom.xml

请帮助找到将两个或更多spring Web项目合并为单个可部署war文件的解决方案。我有以下项目

myproject 
  a. core 
    - src
    - pom.xml
        - packaging = jar
  b. dao 
    - src
    - pom.xml
        - packaging = jar
        - dependency on core project
  c. service
    - src
    - pom.xml 
        - packaging = jar
        - dependency on core and dao projects
  d. salem-web
    - src
    - pom.xml
        - packaging = war
        - dependency on core, dao and service projects

到目前为止一切都很好。但我有一个新的要求,我们可以拥有不同的客户(客户特定的功能)salem-web-customer1,salem-web-customer2等。

  e. service-customer1
    - src
    - pom.xml 
        - packaging = jar
        - dependency on core and dao projects

  f. salem-web-customer1
    - src
    - pom.xml
        - packaging = war
        - dependency on core, dao and service-customer1 projects

我们需要部署 salem-web& salem-web-customer1 作为customer1的一个包等等为其他客户。

我已经尝试过 maven overlays 和其他线程提供的解决方案而没有任何运气。您能否建议将两个Web项目合并为一个正确的配置(如果可能)。

1 个答案:

答案 0 :(得分:4)

有几种可能性,你可以这样做:

有父母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.greg</groupId>
    <artifactId>salem</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>core</module>
        <module>dao</module>
        <module>service</module>
        <module>service-customer1</module>
        <module>salem-web</module>
        <module>salem-web-customer1</module>
    </modules>

</project>

为每个jar和war项目提供具有依赖关系的项目,例如

<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.greg</groupId>
  <artifactId>service-customer1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>service-customer1</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>
    <dependency>
      <groupId>com.greg</groupId>
      <artifactId>core</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.greg</groupId>
      <artifactId>dao</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

And for a war 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.greg</groupId>
        <artifactId>salem</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>salem-web-customer1</artifactId>
    <packaging>war</packaging>

    <name>salem-web-customer1 Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>com.greg</groupId>
            <artifactId>core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.greg</groupId>
            <artifactId>dao</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.greg</groupId>
            <artifactId>service-customer1</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>salem-web-customer1</finalName>
    </build>

</project>

当您构建整个项目时,您将在不同的目标文件夹中获得所需的所有战争:

./dao/target/dao-1.0-SNAPSHOT.jar
./salem-web/target/salam-web.war
./salem-web-customer1/target/salem-web-customer1/WEB-INF/lib/core-1.0-SNAPSHOT.jar
./salem-web-customer1/target/salem-web-customer1/WEB-INF/lib/dao-1.0-SNAPSHOT.jar
./salem-web-customer1/target/salem-web-customer1/WEB-INF/lib/service-customer1-1.0-SNAPSHOT.jar
./salem-web-customer1/target/salem-web-customer1.war
./service/target/service-1.0-SNAPSHOT.jar
./service-customer1/target/service-customer1-1.0-SNAPSHOT.jar