如何在Maven中从父POM生成资源文件?

时间:2016-10-05 10:12:48

标签: java maven

我想让父POM类似于以下内容:

<project>
    <groupId>com.mycompany.whatever</groupId>
    <artifactId>whatever-parent</artifactId>
    <version>1</version>
    <packaging>pom</packaging>
    <build>
        <plugins>
            <!-- plugin for generating resource files -->
            <plugin>
                <groupId>...</groupId>
                <artifactId>...</artifactId>
                <version>...</version>
                <configuration>
                    <files>
                        <file>
                            <!-- location relative to jar's root -->
                            <location>lorem.txt</location>
                            <!-- file contents -->
                            <contents>Lorem ipsum dolor</contents>
                        </file>
                    </files>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>...</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

因此,当它将用作父POM时,lorem.txt文件将自动生成并打包在jar中。

1 个答案:

答案 0 :(得分:0)

  1. 如果您需要在jar / zip文件中包含文件。 请参考使用maven程序集插件的方式:
  2. http://maven.apache.org/plugins-archives/maven-assembly-plugin-2.6/examples/index.html

    http://maven.apache.org/plugins/maven-assembly-plugin/

    EX:

    <assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
              xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
      <id>bin</id>
    
      <formats>
        <format>zip</format>
      </formats>
    
      <fileSets>
    
        <fileSet>
          <directory>src/main/config_templates</directory>
          <lineEnding>unix</lineEnding>
          <outputDirectory>config_templates</outputDirectory>
        </fileSet>
    
        <fileSet>
          <directory>${project.build.directory}</directory>
          <includes>
            <include>dep/*</include>
            <include>classpaths/*</include>
            <include>endorsed/*</include>
            <include>*.jar</include>
          </includes>
          <outputDirectory>target</outputDirectory>
        </fileSet>
    
    1. 如果您是第一次需要项目的结构,请参考从您自己的原型创建项目的方式:
    2. https://maven.apache.org/archetype/maven-archetype-plugin/usage.html

      我希望他们可以帮助你。