Java 根据输入参数在maven中有选择地复制资源?

Java 根据输入参数在maven中有选择地复制资源?,java,maven,maven-plugin,Java,Maven,Maven Plugin,我正在将应用程序从Ant迁移到Maven。此应用程序可以部署到两种环境。一个配置称为Local,另一个称为caller dev,因此自然有两个ant目标。每个ant目标从两个不同的文件夹复制资源,并使其成为最终war文件的一部分 我如何在maven中模仿这种行为?我想在构建时传递一个参数,基于这个参数,我们可以使用maven resources插件复制相应的文件夹。像这样的 <resources> <resource>

我正在将应用程序从Ant迁移到Maven。此应用程序可以部署到两种环境。一个配置称为Local,另一个称为caller dev,因此自然有两个ant目标。每个ant目标从两个不同的文件夹复制资源,并使其成为最终war文件的一部分

我如何在maven中模仿这种行为?我想在构建时传递一个参数,基于这个参数,我们可以使用maven resources插件复制相应的文件夹。像这样的

<resources>
          <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>${local}/**</exclude>
                </excludes>
            </resource>
        </resources>

src/main/resources
真的
${local}/**
我走对了吗?如果是,如何在运行时替换“本地”?另外,我正在做一个补充操作,如果我们想为开发人员构建,那么我将排除本地操作,反之亦然。有没有更好的方法来处理这个问题


另外,如果我使用这个方法,目标war会将整个文件夹复制到其中。如何将maven配置为仅复制文件夹内容而不是整个文件夹。

我建议最好创建如下结构(仅使用属性文件):

创建一个程序集描述符,如下所示,与每个环境的maven程序集插件结合使用:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

  <id>test</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <useProjectArtifact>true</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${basedir}/src/main/environment/test/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

测试
战争
假的
真的
真的
WEB-INF
${basedir}/src/main/environment/test/
**
最后,您需要为每个环境执行:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <id>test</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
      <execution>
        <id>production</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
    </executions>
  </plugin>

org.apache.maven.plugins
maven汇编插件
测试
包裹
单一的
${project.basedir}/src/main/assembly/test.xml
生产
包裹
单一的
${project.basedir}/src/main/assembly/production.xml

结果将是通过一个单一构建获得两个war文件,这两个文件已配置为用于生产,另一个用于开发。

我可以推荐的最好方法是创建如下结构(仅使用属性文件):

创建一个程序集描述符,如下所示,与每个环境的maven程序集插件结合使用:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

  <id>test</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <useProjectArtifact>true</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${basedir}/src/main/environment/test/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

测试
战争
假的
真的
真的
WEB-INF
${basedir}/src/main/environment/test/
**
最后,您需要为每个环境执行:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <id>test</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
      <execution>
        <id>production</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
    </executions>
  </plugin>

org.apache.maven.plugins
maven汇编插件
测试
包裹
单一的
${project.basedir}/src/main/assembly/test.xml
生产
包裹
单一的
${project.basedir}/src/main/assembly/production.xml

结果将是通过一次构建获得两个war文件,其中一个配置用于生产,另一个配置用于开发。

复制资源时,Maven将对定义的
值应用include/exclude模式,并维护该目录下的路径结构。我找不到任何与ant的
flattmapper
等价的东西

其余的,考虑一下。定义包含要在生成时更改的配置的配置文件,然后从命令行激活正确的配置文件。使用如下所示的配置,命令

mvn-Dtarget.env=本地进程资源

将文件从
/src/main/resources/local
复制到
target/classes
。由于“local”包含在资源的
目录
元素中,因此它不会出现在复制资源的路径中

<profiles>
    <profile>
        <id>local</id>
        <activation>
            <property>
                <name>target.env</name>
                <value>local</value>
            </property>
        </activation>
        <properties>
            <resource.dir>local</resource.dir>
        </properties>
    </profile>

    <!-- add a similar profile for dev -->
</profiles>

<resources>
    <resource>
        <directory>src/main/resources/${resource.dir}</directory>
        <filtering>true</filtering>
    </resource>
</resources>

地方的
target.env
地方的
地方的
src/main/resources/${resource.dir}
真的

你可以用我在这里展示的个人资料做更多的事情。我希望这能让您开始,即使它不能解决整个用例。

在复制资源时,Maven会将include/exclude模式应用于定义的
值,并维护该目录下的路径结构。我找不到任何与ant的
flattmapper
等价的东西

其余的,考虑一下。定义包含要在生成时更改的配置的配置文件,然后从命令行激活正确的配置文件。使用如下所示的配置,命令

mvn-Dtarget.env=本地进程资源

将文件从
/src/main/resources/local
复制到
target/classes
。由于“local”包含在资源的
目录
元素中,因此它不会出现在复制资源的路径中

<profiles>
    <profile>
        <id>local</id>
        <activation>
            <property>
                <name>target.env</name>
                <value>local</value>
            </property>
        </activation>
        <properties>
            <resource.dir>local</resource.dir>
        </properties>
    </profile>

    <!-- add a similar profile for dev -->
</profiles>

<resources>
    <resource>
        <directory>src/main/resources/${resource.dir}</directory>
        <filtering>true</filtering>
    </resource>
</resources>

地方的
target.env
地方的
地方的
src/main/resources/${resource.dir}
真的

你可以用我在这里展示的个人资料做更多的事情。我希望这能让您开始,即使它不能解决您的整个用例。

太棒了,但这是否意味着每次我构建时,它都会创建两个war文件。我可以选择生成哪个war文件吗。只要不花太多时间,我就可以使用这个解决方案。太棒了,但这是否意味着每次我构建时,它都会创建两个war文件。我可以选择生成哪个war文件吗。只要它能