【问题标题】:How to include resources from war to another maven project如何将战争中的资源包含到另一个 Maven 项目中
【发布时间】:2012-06-05 10:10:53
【问题描述】:

我有一个 maven 项目,它需要从另一个打包为 war 的 maven 项目中复制 webapp/WEB-INF/ 资源。 我该怎么做 ? 请建议

【问题讨论】:

  • 你能详细说明一下吗?什么样的资源?为什么不能直接包含在第二个项目中?
  • 资源是-ftl文件和xml文件。这些资源对许多项目来说都是通用的,所以我想将它们分开保存,而不是在所有项目中单独添加。

标签: maven maven-2 dependency-management maven-dependency-plugin


【解决方案1】:

我可以看到一些替代方案:

  1. 在版本控制系统中使用外部引用将所有存储库指向相同的文件。
  2. Maven 依赖模块可以复制和解压项目依赖。从那里,您可以使用 Maven 程序集插件(或 Ant 目标)在您自己的安装中包含该依赖项的部分内容。
  3. 至少对于 FTL 文件,也许您可​​以将它们打包到单独的 Jar 文件中,然后通过类加载器将它们作为资源加载。

如果资源被过滤,如果你想要过滤版本,你可能会遇到解决方案1的问题,如果你想要源版本,你可能会遇到问题2、3。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    正如Bittrance所说,你应该使用maven依赖插件。

    更好的方法是创建包含所有共享资源的项目,可能是 zip 类型,它是使用程序集插件构建的。这是好的“行家之道”。这比打开战争的包装更好。

    那么,参考一下

        <dependency>
            <groupId>com.mygroup/groupId>
            <artifactId>my-dependencies</artifactId>
            <version>1.0.0</version>
            <type>zip</type>
        </dependency>
    

    接下来,您使用 maven 依赖插件解压您的资源,在您选择的目录中(可能是 WEB-INF/ ?)

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
           <execution>
               <id>unpack-cfg-test-resources</id>
               <goals>
                    <goal>unpack-dependencies</goal>
               </goals>
               <phase>resources</phase>
                  <configuration>
                    <outputDirectory>${project.build.directory}/WEB-INF/</outputDirectory>
                                    <includeArtifacIds>my-resources</includeArtifacIds>
                    <excludeTypes>pom</excludeTypes>
                    <excludeTransitive>true</excludeTransitive>
                </configuration>
            </execution>
         </executions>
     </plugin>
    

    我不太确定这段代码 sn-p(为其他目的而编写),但这是一个示例。 欲了解更多信息,请点击此链接:http://maven.apache.org/plugins/maven-dependency-plugin/

    如果你不能共享一个包含你的文件的公共项目,你可以解压只包含 ftl(或任何你想要的)的战争,但这不是一个真正干净的解决方案;)

    有很多关于这个主题的帖子: Unzip dependency in maven ... 只需尝试使用关键字 maven-dependency-plugin, unpack :)

    希望对你有所帮助。

    【讨论】:

      【解决方案3】:

      (这假设您的依赖项目是 java (jar) 而不是另一个 web 应用程序,如果它是 webapp 我认为解决方案是相似的)。

      我建议一种(稍微)不同的方法:
      不要从战争中读取资源,而是将其添加到您的战争 pom 中,以在工件和战争中生成一个 jar:

      <!-- maven war plugin config -->
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
          <configuration>
              ...
              <attachClasses>true</attachClasses>
              <classesClassifier>some-string</classesClassifier>
          </configuration>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.0.0</version>
      </plugin>
      ...
      <resources>
      <!-- This is for inclusion in the jar, so dependent module can load it -->
          <resource>
              <targetPath>some-path</targetPath>
              <directory>src/main/webapp/path...</directory>
              <includes>
                  <include>your-resource</include>
              </includes>
          </resource>
      </resources>
      

      这是你消费的 pom,所以生成的 jar 将被加载:

      <dependency>
          <groupId>com.company</groupId>
          <artifactId>...</artifactId>
          <classifier>some-string</classifier>
      </dependency>
      

      然后您将能够以通常的方式加载资源(getResourceAsStream("some-path/your-resource")

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-12
        • 2020-08-20
        • 1970-01-01
        • 2015-10-02
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        相关资源
        最近更新 更多