11

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?

Morfic
  • 15,178
  • 3
  • 51
  • 61
javydreamercsw
  • 5,363
  • 13
  • 61
  • 106
  • Take a look at https://maven.apache.org/plugins/maven-war-plugin/usage.html just to make sure your project structure is correct and double check what gets copied where. – Janez Kuhar Apr 18 '17 at 18:48
  • The maven-compiler plugin automatically runs annotation processors for you. You don't need a special plugin for it. The processor just needs to be in the compiler class path - typically as a `provided` scope dependency. – Steve C Apr 19 '17 at 01:24

3 Answers3

4

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> 
Zilvinas
  • 5,518
  • 3
  • 26
  • 26
2

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>
Jens
  • 20,533
  • 11
  • 60
  • 86
Kaiizok
  • 90
  • 10
2

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.

Community
  • 1
  • 1
javydreamercsw
  • 5,363
  • 13
  • 61
  • 106