Sharing static resources between maven modules

拈花ヽ惹草 提交于 2019-12-22 03:46:31

问题


I have a Maven project with a parent-pom project and 3 maven-module projects in it. 2 of the modules are Java-EE web apps that compile into WAR files. 1 of the modules contains common JAVA code which is shared between the 2 other projects. Sharing the JAVA code was easy.

The question I have is how to a share common static resources such as JavaScript, CSS, and image files without duplicating them in each web module? I would also like to do it in such a way that I can continue running the web app from Eclipse and have changes I make to the static-files automatically available to the Eclipse's running server.


回答1:


Try this:

1.

 |-- pom.xml
    |-- appsweb1 (war)
    |-- appsweb2 (war)
    |-- common (jar)
        |-- src/main/java
        |-- src/static_files
        |-- pom.xml
  1. Add in pom appsweb1, appsweb2, or add it in pom parent and just add groupId, artifactId in child:
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4.2</version>
        <executions>
            <execution>
                <id>default-copy-resources</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <overwrite>false</overwrite>
                    <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/static_files</outputDirectory>
                    <resources>
                        <resource>
                            <directory>../common/src/main/static_files</directory>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

Documentation on the Maven Resources Plugin: https://maven.apache.org/plugins/maven-resources-plugin/




回答2:


Splitting @Eric's question in two, we have:

1) How to share common static resources such as JavaScript, CSS, and image files without duplicating them in each web module?

By the servlet 3.0 specification, you can share resources externally putting them in the source folder src/main/resources. For this to work with dynamic files, like jsp, you should put the files inside the folder META-INF/resources. Then your shared project (jar) structure would look like this:

mymodule
| src
| | main
|   | java
|     | [java code]
|   | resources
|     | META-INF
|       | resources
|         | [your static and dynamic files]

If, for example, your shared js file is in src/main/resources/META-INF/resources/js/myjsfile.js it can be loaded in your html file using the path: <script src="/js/myjsfile.js"></script>. The same approach is valid for your css files.

Additional information: you can include a shared jsp file in your page using <jsp:include page=""/> tag. And, if you are using Spring and have configured your viewResolver's prefix to something like '/WEB-INF/view', you must include your dynamic files inside the specified folders, that is, they would be put in the folder 'src/main/resources/META-INF/resources/WEB-INF/view'.

2) I would also like to do it in such a way that I can continue running the web app from Eclipse and have changes I make to the static-files automatically available to the Eclipse's running server.

Eclipse's servers plugins use the information on the 'deployment assembly' configuration of your project to keep track of the files and automatically publish changes. In your shared project, you can modify the deployment assembly in two ways: you can either a) right click in your project -> properties -> deployment assembly -> add -> folder and then choose the folder containing your files to be deployed and monitored, or b) edit the configuration file '.settings/org.eclipse.wst.common.component', adding something like

<wb-module deploy-name="mymodule">
    <wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module>

If you choose the second approach, be careful, so you don't break your deployment task execution.

Additional information: if you use Maven's resources plugin for filtering resources, you probably want to add your 'target' folder in the deployment assembly, instead your 'src' folder, since the latter will contain non-filtered resources, with your properties - in the format ${my.property} - not set.



来源:https://stackoverflow.com/questions/33096888/sharing-static-resources-between-maven-modules

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