创建项目的不同“分布”

时间:2018-11-05 09:34:04

标签: java maven maven-assembly-plugin

我正在从(或至少试图)从Ant切换到Maven。 我正在构建一个复杂的项目,生成一个WAR文件,并将其扩展到单个文件夹中。

然后,在Ant中,我将创建10个不同的“发行版”,这意味着我将一些属性文件,CSS和HTML文件从“ config /”目录复制到该文件夹​​中,创建一个,然后对下一个“ config”重复该步骤/”文件夹。

最后,我有10个ZIP文件,每个客户都有自定义资源。

和代码看起来像这样(可能不是最好的ant代码,但是效果很好):

<!-- Distributionen -->
<target name="distribution-customer1" depends="jar-with-dependencies">
    <property name="dirname" value="customer1" />
    <antcall target="distribution">
        <param name="dirname" value="${dirname}" />
    </antcall>
</target>
<target name="distribution-customer2" depends="jar-with-dependencies">
    <property name="dirname" value="customer2" />
    <antcall target="distribution">
        <param name="dirname" value="${dirname}" />
    </antcall>
</target>
<target name="distribution-customer3">
    <property name="dirname" value="customer3" />
    <antcall target="distribution">
        <param name="dirname" value="${dirname}" />
    </antcall>
    <!-- Startdateien mit Port 8080 statt Port 80 -->
    <copy todir="${root.dir}/distribution/${dirname}/" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/" includes="myproject_starten**" />
    </copy>
    <!-- Nachdem wir Dateien geändert haben, nochmals WAR und ZIP generieren -->
    <antcall target="create_war_and_zip">
        <param name="dirname" value="${dirname}" />
    </antcall>
</target>
<!-- /Distributionen -->

<!-- Über Antcall aufrufen, nicht direkt! -->
<target name="distribution" depends="jar-with-dependencies">

    <!-- Altes Verzeichnis löschen -->
    <delete dir="${root.dir}/distribution/${dirname}/" />
    <!-- Neu anlegen -->
    <mkdir dir="${root.dir}/distribution/${dirname}/" />
    <!-- Alles vom Template rüber kopieren -->
    <copy todir="${root.dir}/distribution/${dirname}/" overwrite="yes">
        <fileset dir="${root.dir}/distribution/myproject_template/">
        </fileset>
    </copy>
    <!-- Service.ini mit der Version aus dem individuellen Config-Verzeichnis 
        überschreiben -->
    <copy todir="${root.dir}/distribution/${dirname}" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/">
            <include name="*.ini" />
        </fileset>
    </copy>
    <!-- Alte Propertydateien löschen -->
    <copy todir="${root.dir}/distribution/${dirname}/myproject/WEB-INF/classes" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/">
            <include name="*.properties" />
        </fileset>
    </copy>
    <!-- ggf. Log4J Config überschreiben -->
    <copy todir="${root.dir}/distribution/${dirname}/myproject/WEB-INF/classes" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/">
            <include name="*.xml" />
        </fileset>
    </copy>
    <!-- Custom CSS kopieren -->
    <copy todir="${root.dir}/distribution/${dirname}/myproject/assets/css" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/assets/css/">
            <include name="*.css" />
        </fileset>
    </copy>

    <!-- Custom Images kopieren -->
    <copy todir="${root.dir}/distribution/${dirname}/myproject/assets/images" overwrite="yes">
        <fileset dir="${root.dir}/configs/${dirname}/assets/images/">
            <include name="*.png" />
        </fileset>
    </copy>

    <!-- Daten kopieren (Falls vorhanden, für Demo)-->
    <copy todir="${root.dir}/distribution/${dirname}/data" overwrite="yes" failonerror="false">
        <fileset dir="${root.dir}/configs/${dirname}/data/"></fileset>
    </copy>

    <copy todir="${root.dir}/distribution/${dirname}/myproject/bilder" overwrite="yes" failonerror="false">
        <fileset dir="${root.dir}/configs/${dirname}/bilder/"></fileset>
    </copy>

    <delete file="${root.dir}/distribution/${dirname}/readme.internal.md" />

    <antcall target="create_war_and_zip">
        <param name="dirname" value="${dirname}" />
    </antcall>

</target>

<target name="copy-to-distribution" depends="jar-with-dependencies">
    <delete dir="${root.dir}/distribution/myproject_template/myproject/" />
    <mkdir dir="${root.dir}/distribution/myproject_template/myproject/" />
    <mkdir dir="${root.dir}/distribution/myproject_template/myproject/bilder" />
    <mkdir
        dir="${root.dir}/distribution/myproject_template/myproject/bilder/details" />
    <mkdir
        dir="${root.dir}/distribution/myproject_template/myproject/bilder/originals" />
    <mkdir
        dir="${root.dir}/distribution/myproject_template/myproject/bilder/thumbnails" />
    <copy todir="${root.dir}/distribution/myproject_template/myproject/">
        <fileset
            dir="${build.dir}/dist/${ant.project.name}-with-dependencies-jar/" />
    </copy>
</target>

我该如何使用Maven做类似的事情? 我应该使用个人资料吗? 我应该为那个任务使用maven吗?

欢迎任何帮助!

谢谢你, 舒伯

1 个答案:

答案 0 :(得分:1)

您需要使用war-overlays,这是一个示例:

父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>war-overlay-example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>base-war</module>
    <module>dist1-war</module>
  </modules>

</project>

该项目的基本战争,对于任何常见的东西:

<?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>war-overlay-example</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>base-war</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>base-war</finalName>
    </build>

</project>

在基础战争中,有许多分配战争改变了一切。该项目中包含的所有内容都将取代基础战争中的任何内容。您一无所有,无法获得完整的基础战信息或插入单个文件。

<?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>war-overlay-example</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>dist1-war</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>com.greg</groupId>
            <artifactId>base-war</artifactId>
            <version>${project.version}</version>
            <type>war</type>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>dist1-war</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <overlays>
                        <overlay>
                            <groupId>com.greg</groupId>
                            <artifactId>base-war</artifactId>
                        </overlay>
                    </overlays>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

工作示例here