Maven: Where to put generated resources for tomcat-maven-plugin?
Asked Answered
L

3

14

I have CSS and JavaScript files in src/main/webapp directory of my project. I want to join add joined and minified version of these resources to my WAR file and to the place where tomcat-maven-plugin picks it up.

I used yuicompressor-maven-plugin to create the files and put it to ${project.build.directory}/${project.build.finalName}. It works great for maven package and those resources make their way to WAR file, but somehow tomcat-maven-plugin does not see those at all. Should I use a different directory for it?

My pom:

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.1</version>
            <configuration>
                <path>/MyApp</path>
                <warDirectory>${project.build.directory}/${project.build.finalName}</warDirectory>
            </configuration>
        </plugin>
        <plugin>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <optimize>true</optimize>
                <debug>true</debug>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>${basedir}/src/main/resources/META-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>META-INF</targetPath>
                        <includes>
                            <include>context.xml</include>
                        </includes>
                    </resource>
                </webResources>
                <archive>
                    <addMavenDescriptor>false</addMavenDescriptor>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>yuicompressor-maven-plugin</artifactId>
            <version>1.3.0</version>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <configuration>
                        <excludes>
                            <exclude>**/*</exclude>
                        </excludes>
                        <aggregations>
                            <aggregation>
                                <output>${project.build.directory}/${project.build.finalName}/js/commons-pack.js</output>
                                <includes>
                                    <include>${project.build.sourceDirectory}/../webapp/js1.js</include>
                                    <include>${project.build.sourceDirectory}/../webapp/js2.js</include>
                     ...

What should I do to make mvn tomcat:run to also pick up my generated files?

Lipography answered 24/9, 2012 at 18:31 Comment(0)
P
5

Use warSourceDirectory:

<warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>

Instead of this configuration property (warDirectory) for the tomcat-maven-plugin:

<warDirectory>${project.build.directory}/${project.build.finalName}</warDirectory>

According to the tomcat-maven-plugin documentation, warSourceDirectory is where the web resources get picked up, and its default value is ${basedir}/src/main/webapp. This means that if you don’t set that property, you need to generate your unified/minified JavaScript file under ${basedir}/src/main/webapp.

If you set warSourceDirectory to the output folder, this means you need to generate this file before starting Tomcat.

Profess answered 24/9, 2012 at 19:25 Comment(2)
After setting warSourceDirectory website stopped working :) Did I miss something? May be I should move everything to that directory?Lipography
If you set warSourceDirectory to a build folder, you must do a mvn package before hand to actually process the resources.Unopened
S
3

Alternatively, you can also use the run-war goal instead of run, e.g. mvn tomcat6:run-war. This wil use the exploded war in your build directory (and thus filtered resources). See this site. There is also run-war-only which skips the packaging phase.

Stopple answered 12/3, 2013 at 10:53 Comment(0)
F
1

Note the plugin is now maintained at Apache (so upgrade a bit :-) ) see http://tomcat.apache.org/maven-plugin-2.0/.

Even it works using install I'm not sure it's the optimum solution (regarding io and build time).

The tomcat run must be able to use resources from more than one directory (I'm not sure it's possible with the current tomcat embeded api).

Can you add a feature request here https://issues.apache.org/jira/browse/MTOMCAT

Thanks

Ferromagnesian answered 25/9, 2012 at 15:28 Comment(2)
Is it possible to copy all static resources (webapp) to, say, target/ directory and point tomcat there?Lipography
possible using maven.apache.org/plugins/maven-resources-plugin/examples/…Ferromagnesian

© 2022 - 2024 — McMap. All rights reserved.