Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义MavenResourcesFiltering难以实现?_Java_Maven_Maven Resources Plugin_Plexus - Fatal编程技术网

Java 自定义MavenResourcesFiltering难以实现?

Java 自定义MavenResourcesFiltering难以实现?,java,maven,maven-resources-plugin,plexus,Java,Maven,Maven Resources Plugin,Plexus,我想为一些源文件编写一个自定义的资源过滤器,以便预先添加一行 // @sourceURL=<path-to-file> 也许有一些方法可以通过配置来实现我的目标,也许是通过一个。唉,这有点异国情调,也许有人知道类似问题的好例子 也许有一些方法可以通过配置Plexus DI容器,或者通过Plexus组件描述符来实现我的目标 是的,有办法。您需要覆盖DefaultMavenResourcesFiltering使用的“default”MavenFileFilter组件,并提供您自己的组

我想为一些源文件编写一个自定义的资源过滤器,以便预先添加一行

// @sourceURL=<path-to-file> 
也许有一些方法可以通过配置来实现我的目标,也许是通过一个。唉,这有点异国情调,也许有人知道类似问题的好例子

也许有一些方法可以通过配置Plexus DI容器,或者通过Plexus组件描述符来实现我的目标

是的,有办法。您需要覆盖
DefaultMavenResourcesFiltering
使用的
“default”
MavenFileFilter
组件,并提供您自己的组件。这很容易做到:只需为
MavenFileFilter
角色创建自己的Plexus组件,并覆盖
“default”
提示,然后将其添加为资源插件的依赖项。由于这些类直接出现在插件的依赖项下,它们将覆盖默认类

设置如下所示。创建具有以下POM的新Maven项目:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.groupId</groupId>
  <artifactId>my-file-filter</artifactId>
  <version>0.0.1</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven.shared</groupId>
      <artifactId>maven-filtering</artifactId>
      <version>3.1.1</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.plexus</groupId>
        <artifactId>plexus-component-metadata</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <goals>
              <goal>generate-metadata</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
这只需覆盖所有其他方法最终调用的
copyFile
方法,通过调用超级方法,然后执行自定义代码,确保默认行为仍在发生。在本部分中,您可以将
处理为
文件,并在其中准备所需的行

使用
mvn clean install
安装此Maven项目后,您可以将其作为依赖项添加到资源插件中,方法是:

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.0.1</version>
  <dependencies>
    <dependency>
      <groupId>my.groupId</groupId>
      <artifactId>my-file-filter</artifactId>
      <version>0.0.1</version>
    </dependency>
  </dependencies>
</plugin>

maven资源插件
3.0.1
my.groupId
我的文件过滤器
0.0.1
您的自定义文件过滤器将被注入
DefaultMavenResourcesFiltering

Thx中,请参阅以获取参考。在看到您的解决方案之前,我已经完成了它,因此可能可以通过两次写入目标文件(而不是像您建议的那样覆盖+从
DefaultMavenFileFilter
复制和粘贴代码)以及不使用
mavenFilteringHint
来简化它。
@Component(role = MavenFileFilter.class, hint = "default")
public class MyMavenFileFilter extends DefaultMavenFileFilter {

    public void copyFile(File from, File to, boolean filtering, List<FileUtils.FilterWrapper> filterWrappers, String encoding, boolean overwrite) throws MavenFilteringException {
        super.copyFile(from, to, filtering, filterWrappers, encoding, overwrite);
        // your logic of modifying the "to" file here
    }

}
<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.0.1</version>
  <dependencies>
    <dependency>
      <groupId>my.groupId</groupId>
      <artifactId>my-file-filter</artifactId>
      <version>0.0.1</version>
    </dependency>
  </dependencies>
</plugin>