使用Maven将Java模块依赖项资源合并到WAR中

时间:2014-04-07 10:50:30

标签: java html maven war

我有一个多平台项目,它有几个特定于语言的配置文件。特定于HTML平台的模块依赖于" assets-core"模块和如果" en"个人资料已启用," assets-en"模块资源也应包含在WAR中。

组装WAR工作得很好,例外情况是如果" assets-core"模块包含与" assets-en"具有相同文件名的文件,它不会被" assets-en"中的文件覆盖。模块。

我尝试使用maven-resource-plugin:

  <build>
    <resources>
      <resource>
        <directory>${basedir}/assets-en/src/main/resources</directory>
        <includes>
          <include>**/*</include>
        </includes>
      </resource>
    </resources>
  </build>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.3</version>
        <executions>
          <execution>
            <id>copy-prod-resources</id>
            <phase>process-resources</phase>
            <goals>
               <goal>copy-resources</goal>
            </goals>
            <configuration>
              <overwrite>true</overwrite>
              <outputDirectory>${basedir}/target/classes</outputDirectory>
              <resources>
                <resource>
                  <directory>${basedir}/assets-ar/src/main/resources</directory>
                  <includes>
                    <include>**/*</include>
                  </includes>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

和&lt; maven.resources.overwrite&gt; true&lt; /maven.resources.overwrite>像其他线程所暗示的那样启用,但没有任何效果。

maven-war-plugin叠加也可以作为解决方案,但我无法使用&lt; type&gt; war&lt; / type&gt;打包资源模块。因为Java项目中也包含相同的模块。

以下是我的pom.xmls的相关部分:

asssets核/ pom.xml的

<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>example-parent</artifactId>
    <version>1.0</version>
  </parent>

  <artifactId>assets-core</artifactId>
</project>

资产烯/ pom.xml的

<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>example-parent</artifactId>
    <version>1.0</version>
  </parent>

  <artifactId>assets-en</artifactId>
</project>

例如-HTML / pom.xml的

<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>example-parent</artifactId>
    <version>1.0</version>
  </parent>

  <artifactId>example-html</artifactId>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>example-core</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>example-core</artifactId>
      <version>${project.version}</version>
      <classifier>sources</classifier>
    </dependency>

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

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>gwt-maven-plugin</artifactId>
        <version>${gwt.version}</version>
        <executions>
          <execution>
            <phase>prepare-package</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <profiles>
    <profile>
      <id>en</id>

      <dependencies>
        <dependency>
          <groupId>com.example</groupId>
          <artifactId>assets-en</artifactId>
          <version>${project.version}</version>
        </dependency>
      </dependencies>
    </profile>
  </profiles>
</project>

1 个答案:

答案 0 :(得分:2)

好的,所以我终于找到了解决方案。您需要摆脱模块的依赖关系并使用maven-resources-plugin复制资源:

例如-HTML / pom.xml的

<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.example</groupId>
    <artifactId>example-parent</artifactId>
    <version>1.0</version>
  </parent>

  <artifactId>example-html</artifactId>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>example-core</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>example-core</artifactId>
      <version>${project.version}</version>
      <classifier>sources</classifier>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>gwt-maven-plugin</artifactId>
        <version>${gwt.version}</version>
        <executions>
          <execution>
            <phase>prepare-package</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-assets-core</id>
            <phase>process-resources</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/ExampleProject/</outputDirectory>
              <resources>
                <resource>
                  <directory>${project.parent.basedir}/assets-core/src/main/resources/assets</directory>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <profiles>
    <profile>
      <id>en</id>

      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
              <execution>
                <id>copy-assets-en</id>
                <phase>process-resources</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <overwrite>true</overwrite>
                  <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/ExampleProject/</outputDirectory>
                  <resources>
                    <resource>
                      <directory>${project.parent.basedir}/assets-en/src/main/resources/assets</directory>
                    </resource>
                  </resources>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>