为maven pom添加额外资源

时间:2021-07-23 00:01:56

I have a super pom defined in which I specify a "resources" directory for resources. In one of my projects, my pom extends that super pom, and I would like to add an additional resource. I tried:

我有一个超级pom定义,我在其中指定资源的“资源”目录。在我的一个项目中,我的pom扩展了那个超级pom,我想添加一个额外的资源。我试过了:

    <resources>
        <resource>
            <targetPath>/</targetPath>
            <directory>additionalDir</directory>                
        </resource>
    </resources>

But that results in only additionalDir being in the resources and does not include the super pom's resources. Is there a way to extend the super pom's resources?

但这导致只有extraDir在资源中并且不包括超级pom的资源。有没有办法扩展超级pom的资源?

4 个解决方案

#1


The behaviour you've described is expected.

您所描述的行为是预期的。

Remember that your super POM is inheriting from Maven's default POM and pretty much any plugin that you define in your pom effectively overrides the setting in the default POM. As an example, to enable filtering on the default resource, you have to specify the path in your POM. You do this:

请记住,您的超级POM继承自Maven的默认POM,并且几乎您在pom中定义的任何插件都会有效地覆盖默认POM中的设置。例如,要启用对默认资源的过滤,您必须在POM中指定路径。你做这个:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>

Not this:

<resources>
  <resource>
    <filtering>true</filtering>
  </resource>
</resources>

The default directory does not bubble up for you, even if you've specified it there. There might be a special MOJO to do it for you, but I couldn't find one.

默认目录不会为您冒泡,即使您已在那里指定它。可能有一个特殊的MOJO为你做,但我找不到一个。

#2


See build-helper-maven-plugin's usage page.

请参阅build-helper-maven-plugin的用法页面。

#3


Yes, maven overrides the parent POM's resources when redefining new resource section.

是的,maven在重新定义新资源部分时会覆盖父POM的资源。

However, a simple way to add resources is to

但是,添加资源的简单方法是

  1. Remove the resources section from your POM
  2. 从POM中删除资源部分

  3. Show The effective POM
  4. 显示有效的POM

  5. Copy the resources from your effective POM into your project
  6. 将有效POM中的资源复制到项目中

  7. Replace absolute paths with relative ones
  8. 用相对路径替换绝对路径

  9. Add your additional resources
  10. 添加您的其他资源

#4


I know this question is pretty old by now but maybe someone will find this usefull:

我知道这个问题现在已经很老了,但也许有人会觉得这很有用:

You can easily do that using the maven resource plugin.

您可以使用maven资源插件轻松完成此操作。

Basically, what it does by default (invoking the resources:resources goal) is moving the content of all directories listed in the build/resources section using the declared filter into the ${project.build.outputDirectory} If you now want to add some additional resources without influencing these sections, you can simply move the resources there yourself. An no, this does not mean drag and dropping it there.

基本上,它默认执行的操作(调用资源:资源目标)是使用声明的过滤器将build / resources部分中列出的所有目录的内容移动到$ {project.build.outputDirectory}中如果您现在想要添加一些在不影响这些部分的情况下,您可以自行移动资源。不,这并不意味着将它拖放到那里。

The Maven Resources Pluginprovides a goal named resources:copy-resources that does exactly that for you: Coping files using filters from one place to another, or as the plugin doc states:

Maven Resources Plugin提供了一个名为resources的目标:copy-resources,它完全适合您:使用过滤器从一个地方复制文件到另一个地方,或者插件文档说明:

You can use the mojo copy-resources to copy resources which are not in the default maven layout or not declared in the build/resources element and attach it to a phase

您可以使用mojo copy-resources复制不在默认maven布局中或未在build / resources元素中声明的资源,并将其附加到阶段

The obvious advantage compared to the build-helper, that is suggested in other solutions, is that, while the build-helper is only able to take action during the generate-sources phase, the resources plugin works in any phase. (Though it would be wise to execute it before the packaging stage)

与其他解决方案中建议的构建助手相比,明显的优势在于,虽然构建助手只能在生成源阶段采取行动,但资源插件可以在任何阶段工作。 (虽然在包装阶段之前执行它是明智的)

A full example on how to use this is provided here, as part of the plugins project page.

这里提供了有关如何使用它的完整示例,作为插件项目页面的一部分。

What this does is coping the content of the not listed resource directory "src/non-packaged-resources" into "${basedir}/target/extra-resources". But since the jar plugin (that creates the jar archive) basically creates a zip archive of the directory "${project.build.outputDirectory}", you may want to put it there.

这样做是将未列出的资源目录“src / non-packaged-resources”的内容复制到“$ {basedir} / target / extra-resources”中。但是因为jar插件(创建jar存档)基本上创建了目录“$ {project.build.outputDirectory}”的zip存档,所以你可能想把它放在那里。

#1


The behaviour you've described is expected.

您所描述的行为是预期的。

Remember that your super POM is inheriting from Maven's default POM and pretty much any plugin that you define in your pom effectively overrides the setting in the default POM. As an example, to enable filtering on the default resource, you have to specify the path in your POM. You do this:

请记住,您的超级POM继承自Maven的默认POM,并且几乎您在pom中定义的任何插件都会有效地覆盖默认POM中的设置。例如,要启用对默认资源的过滤,您必须在POM中指定路径。你做这个:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>

Not this:

<resources>
  <resource>
    <filtering>true</filtering>
  </resource>
</resources>

The default directory does not bubble up for you, even if you've specified it there. There might be a special MOJO to do it for you, but I couldn't find one.

默认目录不会为您冒泡,即使您已在那里指定它。可能有一个特殊的MOJO为你做,但我找不到一个。

#2


See build-helper-maven-plugin's usage page.

请参阅build-helper-maven-plugin的用法页面。

#3


Yes, maven overrides the parent POM's resources when redefining new resource section.

是的,maven在重新定义新资源部分时会覆盖父POM的资源。

However, a simple way to add resources is to

但是,添加资源的简单方法是

  1. Remove the resources section from your POM
  2. 从POM中删除资源部分

  3. Show The effective POM
  4. 显示有效的POM

  5. Copy the resources from your effective POM into your project
  6. 将有效POM中的资源复制到项目中

  7. Replace absolute paths with relative ones
  8. 用相对路径替换绝对路径

  9. Add your additional resources
  10. 添加您的其他资源

#4


I know this question is pretty old by now but maybe someone will find this usefull:

我知道这个问题现在已经很老了,但也许有人会觉得这很有用:

You can easily do that using the maven resource plugin.

您可以使用maven资源插件轻松完成此操作。

Basically, what it does by default (invoking the resources:resources goal) is moving the content of all directories listed in the build/resources section using the declared filter into the ${project.build.outputDirectory} If you now want to add some additional resources without influencing these sections, you can simply move the resources there yourself. An no, this does not mean drag and dropping it there.

基本上,它默认执行的操作(调用资源:资源目标)是使用声明的过滤器将build / resources部分中列出的所有目录的内容移动到$ {project.build.outputDirectory}中如果您现在想要添加一些在不影响这些部分的情况下,您可以自行移动资源。不,这并不意味着将它拖放到那里。

The Maven Resources Pluginprovides a goal named resources:copy-resources that does exactly that for you: Coping files using filters from one place to another, or as the plugin doc states:

Maven Resources Plugin提供了一个名为resources的目标:copy-resources,它完全适合您:使用过滤器从一个地方复制文件到另一个地方,或者插件文档说明:

You can use the mojo copy-resources to copy resources which are not in the default maven layout or not declared in the build/resources element and attach it to a phase

您可以使用mojo copy-resources复制不在默认maven布局中或未在build / resources元素中声明的资源,并将其附加到阶段

The obvious advantage compared to the build-helper, that is suggested in other solutions, is that, while the build-helper is only able to take action during the generate-sources phase, the resources plugin works in any phase. (Though it would be wise to execute it before the packaging stage)

与其他解决方案中建议的构建助手相比,明显的优势在于,虽然构建助手只能在生成源阶段采取行动,但资源插件可以在任何阶段工作。 (虽然在包装阶段之前执行它是明智的)

A full example on how to use this is provided here, as part of the plugins project page.

这里提供了有关如何使用它的完整示例,作为插件项目页面的一部分。

What this does is coping the content of the not listed resource directory "src/non-packaged-resources" into "${basedir}/target/extra-resources". But since the jar plugin (that creates the jar archive) basically creates a zip archive of the directory "${project.build.outputDirectory}", you may want to put it there.

这样做是将未列出的资源目录“src / non-packaged-resources”的内容复制到“$ {basedir} / target / extra-resources”中。但是因为jar插件(创建jar存档)基本上创建了目录“$ {project.build.outputDirectory}”的zip存档,所以你可能想把它放在那里。