简体   繁体   中英

Vaadin: How to add META-INF/services to the war?

I have a Vaadin 7 maven web project that has some annotations that create service definition on META-INF/services .

I added this to the pom so the annotations are processed:

<!-- 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>

The files show within target/classes/META-INF/services but don't make it to the final war.

I tried adding the folder to the maven-war-plugin like this:

<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>

But then most of the Vaadin files don't make it into the war and it doesn't work. Any idea?

You can try this:

<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>

Please see https://maven.apache.org/plugins/maven-war-plugin/usage.html

It looks like your META-INF folder is in src/main/resources directory.

Normally when creating a WAR you would create a directory src/main/webapp where you can have your WEB-INF , META-INF and other required folders.

 |-- src
 |   `-- main
 |       |-- java
 |       |   `-- com
 |       |       `-- example
 |       |           `-- projects
 |       |               `-- SampleAction.java
 |       |-- resources
 |       |   `-- images
 |       `-- webapp
 |           |-- META-INF
 |           |-- WEB-INF

Everything from your webapp folder will be copied over to the root dir in the WAR .

The path to your webapp can also be configured using warSourceDirectory property

For your scenario - generated sources

Obviously you don't want to keep your generated sources in your src/* folder and don't want to version it.

You can get around this by either adding an ignore filter to your version control metadata; or by creating a symlink; or using copy-resources as some previous answers said, but not recommended.

You can achieve this by adding a webResources configuration to copy the generated sources from target folder by

Please see 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> 

I ended up using the approach from this unrelated answer: Maven: include folder in resource folder in the war build .

Here's what I ended up doing:

             <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>

Basically added the services folder as a resource and placed it on the right place of the final war.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM