Building EAR from a single Maven project

给你一囗甜甜゛ 提交于 2019-11-27 08:40:22

问题


There is a Maven project that used to have JAR packaging type. Now the project has evolved, and it is required that it be built as EAR, including the JAR proper and its dependencies altogether.

Does it necessarily mean I have to introduce the second POM with EAR packaging type? Is it possible to build JAR+EAR from a single Maven project? The reason behind that is that I don't really want to radically reorganize my repository structure.

If not possible, I guess I'd need to somehow reorganize project structure to make use of Maven modules. What (sub)module structure would you recommend in that case?


回答1:


You will need 3 pom, here is a working example;

pom 1 is the parent

<?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 is the jar or war

<?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 is the ear which has a dependency to the jar/war

<?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>



回答2:


I think you can use maven war plugin to generate a single war file every time you build the project

 <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>



来源:https://stackoverflow.com/questions/41720479/building-ear-from-a-single-maven-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!