Maven - 使用Maven描述符文件附加工件

时间:2017-04-03 12:52:11

标签: maven

出于性能原因,我需要在WAR存档旁边创建一个新的EAR工件,而不使用多个模块projekt 。 EAR将包含WAR(在/web子目录中)和静态application.xml文件。

我需要以非静态方式生成的是Java类和 Maven描述符文件META-INF/maven/${groupId}/${artifactId}/pom.xmlMETA-INF/maven/${groupId}/${artifactId}/pom.properties)。

使用类没什么大不了的,但是如何生成Maven描述符文件并将其附加到新的EAR?

这很有效,但是在EAR存档中不会生成Maven描述符文件。

...
<packaging>war</packaging>
...
<properties>
  <compile.dir>${project.build.directory}/${project.build.finalName}</compile.dir>
  <ear.zip>${project.build.directory}/my-archive.ear</ear.zip>
</properties>
...
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <id>create-EAR-archive</id>
        <phase>prepare-package</phase>
        <goals>
            <goal>run</goal>
        </goals>
        <configuration>
            <target>
              <!-- copy webapp files -->
              <copy todir="${compile.dir}/web">
                  <fileset dir="src/main/webapp" />
              </copy>
              <!-- copy classes -->
              <copy todir="${compile.dir}/web/WEB-INF/classes">
                  <fileset dir="${project.build.directory}/classes" />
              </copy>
              <!-- copy application.xml -->
              <copy todir="${compile.dir}">
                  <fileset dir="src/main/resources" includes="META-INF/application.xml" />
              </copy>
              <!-- copy some big files -->
              ...
              <!-- zip EAR -->
              <zip destfile="${ear.zip}" basedir="${compile.dir}" />
            </target>
         </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <id>attach-EAR-artifact</id>
            <goals>
                <goal>attach-artifact</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <artifacts>
                    <artifact>
                        <file>${ear.zip}</file>
                        <type>ear</type>
                        <classifier>exploded</classifier>
                    </artifact>                                
                </artifacts>
                <archive>
                  <addMavenDescriptor>true</addMavenDescriptor>
                </archive>
            </configuration>
        </execution>
      </executions>
  </plugin>
</plugins>

2 个答案:

答案 0 :(得分:0)

最简单的方法是拥有一个多模块项目。

父母喜欢:

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

  <properties>
        <myproject.version>1.0-SNAPSHOT</myproject.version>
  </properties>

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

</project>

你有一场战争和耳朵项目:

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

然后只需将您需要的任何文件添加到ear项目的src / main / application目录中,它们将显示在ear文件的顶层。

请参阅http://maven.apache.org/plugins/maven-ear-plugin/usage.html

答案 1 :(得分:0)

解决方案是在准备包阶段之前将war包阶段移动到另一个阶段,然后有一个包含maven描述符文件的完整WAR存档。这意味着,准备好EAR档案时,所有数据都已准备就绪。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <executions>
        <execution>
        <phase>process-classes</phase>
        <goals>
            <goal>war</goal>
        </goals>
        </execution>
    </executions>                    
</plugin>

存档解压缩并在准备包阶段中使用,如前所述:

<!-- unzip the war -->
<unzip src="${project.build.directory}/${project.build.finalName}.war" dest="${project.build.directory}/war-exploded" />                                

<!-- move the war content -->
<move todir="${compile.dir}/web">
    <fileset dir="${project.build.directory}/war-exploded" />
</move>