Maven module for static files

心已入冬 提交于 2019-12-06 13:01:25

问题


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?


回答1:


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.




回答2:


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



回答3:


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?



来源:https://stackoverflow.com/questions/34209890/maven-module-for-static-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!