【问题标题】:Howto copy README.md into src/main/resources?如何将 README.md 复制到 src/main/resources 中?
【发布时间】:2015-04-18 03:41:57
【问题描述】:

我想在我的网络应用程序中显示README.md 文件之类的帮助页面。为了不创建重复,我需要通过mvn从项目路径复制到资源中。

我该怎么做?

有什么想法吗?

我试过了:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.3</version>

    <configuration>
        <!-- this is important -->
        <overwrite>true</overwrite>
        <!-- target -->
        <outputDirectory>${basedir}/target/classes</outputDirectory>
        <resources>
            <resource>
                <!-- source -->
                <directory>/</directory>
                <include>
                    <filter>**/README.md</filter>
                </include>
            </resource>
        </resources>
    </configuration>
</plugin>

【问题讨论】:

  • 只是把它们放在你的资源文件夹是不行的?
  • &lt;directory&gt;/&lt;/directory&gt; 很可疑,试试&lt;directory&gt;${basedir}&lt;/directory&gt;

标签: java maven maven-plugin maven-resources-plugin


【解决方案1】:

最简单的解决方案是将适当的文件移动到src/main/resources 文件夹,而第二种解决方案可能是这样的:

  <build>
    <resources>
      <resource>
        <directory>${project.basedir}</directory>
        <includes>
          <include>README.md</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>

无需配置maven-resources-plugin,这可以通过通常的生命周期和资源处理来处理。您可能需要调整的唯一内容是README.md 所在的文件夹。如果您喜欢过滤,还需要添加 &lt;filtering&gt;true&lt;/filtering&gt; 部分。

在构建过程中通过 Maven 复制某些内容到 src/** 中通常是一个坏主意,因为这些文件夹受版本控制系统控制,这将导致您不希望进行未提交的更改。

注意:最好检查插件的最新版本(因为 2.3 是 2008 年的!)。当前插件版本列表可以在这里找到:http://maven.apache.org/plugins/

【讨论】:

  • 我猜当前应该是 2.7 ;)
【解决方案2】:

这适用于我的一个项目:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <!-- when to execute copy operation -->
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>${basedir}/pathTo.MD</directory>
                    </resource>
                </resources>
                <overwrite>true</overwrite>
                <outputDirectory>${project.build.directory}/${project.build.finalName}/classes</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

我看到我从你的版本中获得了额外的阶段和目标。我还使用变量作为输出位置。

【讨论】:

  • 谢谢,我更喜欢@khmarbaise 解决方案
  • 我是这样使用它的,因为我只根据一些配置文件复制该资源。
  • 我明白了。就我而言,我需要始终复制它。谢谢解释
【解决方案3】:

我会建议你更平等地使用这个例子:http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html “更平等”的意思是喜欢使用&lt;executions&gt; 标签。

您当然可以省略 &lt;id&gt;filtering 之类的内容。

以下对我来说很好用:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>myID</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
              <overwrite>true</overwrite>
              <resources>          
                <resource>
                  <directory>src</directory>
                  <!-- Details about filtering: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html -->
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

【讨论】:

    猜你喜欢
    • 2012-07-31
    • 1970-01-01
    • 2012-05-06
    • 2014-06-26
    • 2015-03-22
    • 2014-09-25
    • 2016-08-22
    • 1970-01-01
    • 2014-03-25
    相关资源
    最近更新 更多