Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 583
javydreamercsw Avatar asked Apr 12 '17 14:04

javydreamercsw


3 Answers

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.

like image 158
javydreamercsw Avatar answered Oct 21 '22 09:10

javydreamercsw


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> 
like image 43
Zilvinas Avatar answered Oct 21 '22 11:10

Zilvinas


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>
like image 36
Kaiizok Avatar answered Oct 21 '22 09:10

Kaiizok