Vaadin:如何将META-INF/services添加到war文件中?

11

我有一个使用Vaadin 7的Maven Web项目,其中包含一些注释,在META-INF/services上创建服务定义。

我添加了这个到pom文件中,以便处理注释:

<!-- Run annotation processors on src/main/java sources -->
<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>3.3.1</version>
    <executions>
        <execution>
            <id>process</id>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
        </execution>
    </executions>
</plugin>

这些文件位于target/classes/META-INF/services,但没有出现在最终的war包中。

我尝试将该文件夹添加到maven-war-plugin中,方法如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <packagingIncludes>target/classes/*</packagingIncludes>
    </configuration>
</plugin>
但是大多数Vaadin文件都没有被打包进war包里,导致无法正常工作。有什么想法吗?

请查看 https://maven.apache.org/plugins/maven-war-plugin/usage.html,确保您的项目结构正确,并仔细检查要复制到何处。 - Janez Kuhar
maven-compiler插件会自动为您运行注解处理器,您不需要特殊的插件。处理器只需要在编译器类路径中 - 通常作为“provided”范围依赖项即可。 - Steve C
3个回答

4
请查看https://maven.apache.org/plugins/maven-war-plugin/usage.html
看起来你的META-INF文件夹在src/main/resources目录中。
通常在创建WAR时,您会创建一个名为src/main/webapp的目录,在其中可以包含WEB-INFMETA-INF和其他必需的文件夹。
 |-- src
 |   `-- main
 |       |-- java
 |       |   `-- com
 |       |       `-- example
 |       |           `-- projects
 |       |               `-- SampleAction.java
 |       |-- resources
 |       |   `-- images
 |       `-- webapp
 |           |-- META-INF
 |           |-- WEB-INF

你的webapp文件夹中的所有内容都将复制到WAR的根目录中。

还可以使用warSourceDirectory属性配置webapp的路径。

针对你的场景-生成的源代码

显然,你不想将生成的源代码保存在src/*文件夹中也不想对其进行版本控制。

你可以通过向版本控制元数据添加忽略过滤器、创建符号链接或使用copy-resources(不建议)来解决此问题。

你可以通过添加一个webResources配置来将从target文件夹中复制生成的源代码。

请参见http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html

<plugin> 
  <groupId>org.apache.maven.plugins</groupId> 
  <artifactId>maven-war-plugin</artifactId> 
  <configuration> 
    <webResources> 
      <resource> 
        <directory>${project.build.directory}/target/generated-sources</directory> 
        <targetPath>META-INF</targetPath> <!-- introduced in plugin v 2.1 -->
        <includes> 
          <include>*.class</include> 
        </includes> 
      </resource> 
    </webResources> 
  </configuration> 
</plugin> 

在我的情况下,它不是生成的源代码。这是一个服务定义文件,但这已经足够接近了。 - javydreamercsw

2
你可以尝试这个方法:
<execution>
    <id>process</id>
    <phase>generate-sources</phase>
    <goals>
        <goal>process</goal>
    </goals>
    <configuration>
        <finalName>${projectId}</finalName>
        <outputDirectory>relative project directory</outputDirectory>
        <includes>
            <include>path</include>
        </includes>
    </configuration>
</execution>

2
我最终采用了这个无关回答中的方法:Maven:在war构建中将文件夹包含在资源文件夹中
这是我最终做的事情:
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <webResources>
                        <resource>
                            <directory>target/classes/META-INF/services</directory>
                            <includes>
                                <include>*.*</include>
                            </includes>
                            <targetPath>META-INF/services</targetPath>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>

基本上将服务文件夹添加为资源,并将其放置在最终war包的正确位置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接