1

I have a web application with spring and maven. The application is divided into maven modules. I have an "application-web" module that generates war. Another "application-ear" module that generates an ear. And another "application-static" with css, js and images that generates a zip file.

This would be the outline of my application:

  • application
    • application-web (java code)
    • application-ear
    • application-static (css, js and images)
    • application-resources (language properties)

I want to deploy the eclipse use files of "static" module, instead of using the files in the "application/src/main/webapp" directory. How can I do this? It is possible?

oscar
  • 1,636
  • 6
  • 31
  • 59
  • 2
    Why have you move the css/js and images into application-static instead directly into application-web module? – khmarbaise Dec 10 '15 at 20:17
  • These are the specifications of the project. A module for static files, one for internationalization messages and another for configuration files settings. – oscar Dec 10 '15 at 21:13
  • This works but I have another problem. – oscar Dec 11 '15 at 10:08

3 Answers3

2

Don't waste your time with unpacking resources, everything works out of the box:

  1. Resource bundles: the are always loaded from the classpath, regardlessly it is exploded (WEB-INF/classes) or compressed (WEB-INF/lib)
  2. Serving static resources: bundle them up in a JAR and use either Tomcat (Servlet 3.0) feature to serve from META-INF/resources, see here or use Spring's built in mvc:resources element.

Simply add the dependency snippets in your WAR POM and your are done.

Community
  • 1
  • 1
Michael-O
  • 18,123
  • 6
  • 55
  • 121
  • I have the application-static dependecy in the application-web pom and remove de build secction. I remove the static files of src/main/webapp directory. How to get the static files with mvc: resources? Before I use because the files was in application-web module. – oscar Dec 11 '15 at 11:44
  • @oscar `location` is using default resource paths applied in Spring. I.e., since you are in a webapp default locations are resolved agaist the webapp, to get resources from JARs, use `classpath:/assets/`. – Michael-O Dec 11 '15 at 11:49
  • Not working for me, the application-static module generate a zip not a jar. To deploy in eclipse the static files not found. Inside application-web war not found the zip or static files. – oscar Dec 11 '15 at 12:02
  • @oscar Use a standard JAR and it will work. It doesn't make any sense in this environment to use ZIP files. – Michael-O Dec 11 '15 at 12:17
  • If use jar file, the file(application-static.jar) is copy to WEB-INF/lib directory but not find the static files. thanks your patience i`m a noob with maven and packing modules. – oscar Dec 11 '15 at 12:57
  • @oscar Show your Spring config. – Michael-O Dec 11 '15 at 13:11
0

You can use the maven dependency plugin to unpack your static resources included in the zip file using the following:

In your application-web.pom

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack-resources</id>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <outputDirectory>resources</outputDirectory>
                <includeArtifactIds>application-static</includeArtifactIds>
                <includeGroupIds>your group id</includeGroupIds>
                <includes>**/*.js,**/*.css</includes>
            </configuration>
        </execution>
    </executions>
</plugin
Taf
  • 258
  • 2
  • 8
0

This work:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>unpack-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <configuration>
                        <excludeTypes>pom</excludeTypes>
                        <includeArtifactIds>application-static</includeArtifactIds>
                        <includeGroupIds>com.foo.foo.application</includeGroupIds>
                        <outputDirectory>src/main/webapp</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}-${project.version}</finalName>
</build>

But I have another problem, with this solution only copy the static files when I execute clean package. If I run the application-web with eclipse and I do changes in a js file in application-static, the changes have no effect until I execute clean package. Any solution for this?

oscar
  • 1,636
  • 6
  • 31
  • 59