2017-01-18 99 views
0

有一個Maven項目曾經有JAR包裝類型。現在該項目已經發展,並且要求它被構建爲EAR,包括JAR本身及其依賴項。從單個Maven項目構建EAR

這是否意味着我必須引入與EAR包裝類型的第二個POM?是否可以從單個Maven項目構建JAR + EAR?這背後的原因是,我並不想從根本上重新組織我的存儲庫結構。

如果不可能,我想我需要以某種方式重新組織項目結構以使用Maven模塊。在這種情況下,你會推薦什麼(子)模塊結構?

+0

一般來說,你可以但不是一個好主意......更好的做法是用耳朵包裝創建一個模塊,一個使用罐子包裝和父母將所有這些結合在一起......分離關注點...... – khmarbaise

回答

1

您將需要3 POM,這裏是一個工作示例;

POM 1是父

<?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>ear-example</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>pom</packaging> 

    <name>ear-example</name> 
    <modules> 
    <module>example-jar</module> 
    <module>example-ear</module> 
    </modules> 

</project> 

POM 2是罐子或戰爭

<?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.greg</groupId> 
    <artifactId>ear-example</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    </parent> 

    <artifactId>example-jar</artifactId> 
    <packaging>jar</packaging> 

    <name>com.greg</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> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    </dependencies> 
</project> 

POM 3是具有一個依賴於罐/戰爭

<?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.greg</groupId> 
    <artifactId>ear-example</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    </parent> 

    <artifactId>example-ear</artifactId> 
    <packaging>ear</packaging> 

    <dependencies> 
    <dependency> 
     <groupId>com.greg</groupId> 
     <artifactId>example-jar</artifactId> 
     <version>${project.version}</version> 
    </dependency> 
    </dependencies> 

    <build> 
    <plugins> 
    <plugin> 
     <artifactId>maven-ear-plugin</artifactId> 
     <version>2.10.1</version> 
     <configuration> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 

</project> 
0

我想你可以使用Maven插件戰爭產生一個戰爭文件每次生成項目

<build> 
<plugins> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-ear-plugin</artifactId> 
    <version>2.3.2</version> 
    <!-- configuring the ear plugin --> 
    <configuration> 
     <modules> 
     <!-- all your modles --> 
     </modules> 
    </configuration> 
    </plugin> 
</plugins>