12

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.

Eric
  • 6,563
  • 5
  • 42
  • 66
  • Hi @Eric, I'm a bit late lol, but hopefully I can help other people facing the same issue, who come to check this later. Please, take a look at my answer below and give me feedback. – Ismael Sarmento Oct 01 '19 at 04:00

2 Answers2

6

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/

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
question_maven_com
  • 2,457
  • 16
  • 21
  • 1
    Would I have to run this every time I edit the static files or would Eclipse be smart enough to automatically move the file whenever it changes? I am worried about rapidly being able to develop/test changes I make to the files. – Eric Oct 13 '15 at 14:44
  • I am familiar with using/creating WAR files, but I have never heard of WebJars. Can you point me to an example or documentation on how to use it? – Eric Nov 05 '15 at 16:11
  • hi, could you please explain more about the case put the configuration in parent pom and read them in child pom? Thank you very much – Ock Oct 17 '17 at 14:02
  • add it at pluginManagement in parent-pom and use simply maven-resources-plugin> in child, u can also use + inherited=false – question_maven_com Oct 17 '17 at 14:08
  • In this case, you should be familiar with an overriding default process with default-copy-resources so you will get in the final package only files and folder which you specify in this configuration. – Peter S. Apr 28 '20 at 09:45
0

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.

Ismael Sarmento
  • 844
  • 1
  • 11
  • 22