0

I don't know why but maven-war-plugin stopped moving my files to WEB-INF/config. This is how I am using the plugin:

<profiles>
    <profile>       
        <id>default-profile</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>   
        <build>
            <pluginManagement> 
                <plugins>   
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.6</version>
                        <configuration>
                            <webResources>
                                <resource>
                                    <directory>${basedir}/src/main/assembly/${package-mode}/config</directory>
                                    <filtering>true</filtering>
                                    <targetPath>WEB-INF/config</targetPath>
                                </resource>
                            </webResources>
                        </configuration>
                    </plugin>  
                </plugins>
            </pluginManagement>
        </build>    
    </profile>
</profiles>

Question: What could I possible have done to break this again? It finally worked, now it doesn't anymore.

This is how I run my server:

mvn tomcat7:run-war -Denv=dev -Dpackage-mode=dev -DskipTests

The WAR file gets build and packed but those files from src/main/assembly/dev/config are simply not there.

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • off-topic: Please, never use profiles to make environment specific artifacts. The concept is to always have the same artifact no matter the arguments. For your purpose consider using https://github.com/khmarbaise/multienv-maven-plugin for a clean solution. – Robert Scholte Jun 04 '16 at 11:42
  • @displayname I see that you are defining `maven-antrun-plugin` in your pom with a comment which i can't believe? For what is the maven-antrun-plugin needed ? – khmarbaise Jun 04 '16 at 15:19
  • @khmarbaise Oh, this is actually unrelated to this particular question here but I had to add it [for another reason](http://stackoverflow.com/questions/37601777/why-is-my-target-not-getting-executed). Until now I have changed my pom.xml and I am using `maven-war-plugin` instead of `maven-antrun-plugin`. However, this comment turned out to be true strangely. – Stefan Falk Jun 04 '16 at 15:43
  • Based on the other question you trying to violate the convention over configuration paradigm which results into such problems you have. Doing things like this via antrun is simply wrong... – khmarbaise Jun 04 '16 at 15:50
  • @khmarbaise I didn't use Maven for more than dependency management - everything beyond that is unfortunately over my head at the moment. Using antrun seemed reasonable for me but after A. Di Matteo suggested maven-war-plugin I realized that it's definitely the better choice. Speaking of "what plugin to use" what if I need to "inject" resources from a jar (dependency) into my WAR - any suggestions on a plugin here? ^^ – Stefan Falk Jun 04 '16 at 15:58
  • If you have resources in a jar where this jar is packaged into the WAR which means it is on the classpath of the WAR so no need to unpack the resources ? Maybe those resources belong more to the war than in the jar file? – khmarbaise Jun 04 '16 at 16:09
  • @khmarbaise Well, this jar file is my data model. This data model is supposed to get used by different modules which may implement their own repository layer. Further I am using FlywayDB which is looking for `db/migrate` in the classpath in order to run new .sql scripts. Any unseen script is getting executed and the new database is versioned but in order to do that, the web server needs to see those in `db/migrate`. – Stefan Falk Jun 04 '16 at 16:16
  • What do you mean by data model which is used by the WAR ? The classes you have created for it? – khmarbaise Jun 04 '16 at 20:16
  • @khmarbaise yes. It's the generated Java POJOs for accessing the database. They are getting used in my repository layer. – Stefan Falk Jun 04 '16 at 22:47
  • So this means it's a separate module which contains only the Java POJO's. So why do you need the resources? – khmarbaise Jun 05 '16 at 10:08
  • @khmarbaise The server main module is using FlywayDB. FlywayDB is versioning the state of the database and essentially just looks if there is a new `.sql` file in the classpath `db/migration`, and if so, executes that script. This means that the main module is required to have that file in its classpath (I don't know if this can be done easier tbh). In order to do that I am currently unpacking the the `data-model.jar` and copy those files into the `resources/` directory of the server main module which then moves those files into the WAR. I know that's not nice but I don't know how to do better – Stefan Falk Jun 05 '16 at 10:20
  • As I already set. Packaging a jar into WAR means that jar is on the classpath. So no need to do unpack. – khmarbaise Jun 05 '16 at 11:04

1 Answers1

1

Running the minimal pom below according to your question:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample</groupId>
    <artifactId>web-sample</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>default-profile</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-war-plugin</artifactId>
                            <version>2.6</version>
                            <configuration>
                                <webResources>
                                    <resource>
                                        <directory>${basedir}/src/main/assembly/${package-mode}/config</directory>
                                        <filtering>true</filtering>
                                        <targetPath>WEB-INF/config</targetPath>
                                    </resource>
                                </webResources>
                            </configuration>
                        </plugin>
                        <plugin>
                            <groupId>org.apache.tomcat.maven</groupId>
                            <artifactId>tomcat7-maven-plugin</artifactId>
                            <version>2.0</version>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
    </profiles>
</project>

And executing Maven as below:

mvn tomcat7:run-war -Dpackage-mode=dev

Files got copied corrently, as also specified by the build log:

[INFO] --- maven-war-plugin:2.6:war (default-war) @ web-sample ---   
[INFO] Packaging webapp   
[INFO] Assembling webapp [web-sample] in [C:\Development\workspace-mars\web-sample\target\web-sample]   
[INFO] Processing war project   
[INFO] Copying webapp webResources [C:\Development\workspace-mars\web-sample/src/main/assembly/dev/config] to [C:\Development\workspace-mars\web-sample\target\web-sample]   
[INFO] Copying webapp resources [C:\Development\workspace-mars\web-sample\src\main\webapp]   
[INFO] Webapp assembled in [106 msecs]   
[INFO] Building war: C:\Development\workspace-mars\web-sample\target\web-sample.war   
[INFO]     
[INFO] <<< tomcat7-maven-plugin:2.0:run-war (default-cli) < package @ web-sample <<<   

Hence the error is somewhere else in your project or in your POM configuration.

I would suggest to run a

mvn clean

And retry. Also, to run a

mvn clean package

And check whether files are copied or not, regardless of the tomcat7 plugin execution.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • I really don't understand why it worked yesterday when I had it in a `` ... – Stefan Falk Jun 04 '16 at 12:27
  • According to your pom, you are defining it in an activated-by-default profile AND in the pluginManagement, why? – A_Di-Matteo Jun 04 '16 at 12:28
  • Okay, now I see you put it into a profile as well. I already moved it into build/plugins and it works - I guess I'll leave it that way :) – Stefan Falk Jun 04 '16 at 12:33
  • Is there a chance that you know how I can copy resources from a jar dependency into the final WAR as well? ^^ – Stefan Falk Jun 04 '16 at 12:33
  • check [this SO post](http://stackoverflow.com/q/5388661/5606016), basically you can unpack the dependency in your target folder, then copy resources from there – A_Di-Matteo Jun 04 '16 at 12:36