如何在 Maven 和 Eclipse 中管理多个 Web 应用程序的共享资源?

发布于 2024-10-16 22:26:55 字数 4359 浏览 7 评论 0原文

注意:我在这个问题的第一个版本中没有得到任何答复,所以我将其修改为更通用...

Context

我的项目分为几个maven模块和几个网络应用程序。结构如下:

my-project
  + pom.xml
  +-- commons
  +-- persistence
  +-- ...
  +-- web-app-1
  +-- web-app-2
  +-- ...

所有 Web 应用程序共享公共资源,例如 JS、CSS 和图像文件。

我决定创建另一个名为 web-resources 的项目,这是一个 WAR 项目,而不是在每个 web-app-X 中复制这些资源。 结构如下:

my-project
  + pom.xml
  +-- commons
  +-- persistence
  +-- ...
  +-- web-app-1
  +-- web-app-2
  +-- ...
  +-- web-resources
       +-- pom.xml
       +-- src/main/webapp
            +-- web.xml (which is almost empty, just need to be present for Maven)
            +-- web_resources
                 +-- css
                 +-- images
                 +-- javascript

Maven

在 Maven 2(或 Maven 3,因为我刚刚将项目迁移到 Maven 3.0.2)中,此配置很容易管理,因为所有 web- app-Xweb-resources 声明为依赖项:

<groupId>foo.bar</groupId>
<artifactId>web-app-1</artifactId>
...
<dependencies>
    <dependency>
        <groupId>foo.bar</groupId>
        <artifactId>web-resources</artifactId>
        <version>${preclosing-version}</version>
        <type>war</type>
    </dependency>
    ...

因此,当我构建 WAR 项目时,它首先获取 web-resources.war (在之前构建) ),解压缩它,并在其上构建 web-app-X Web 应用程序。 这样,我的 WAR 文件还将包含一个名为 web-resources/ 的目录,其中包含共享资源。

这就是战争叠加原理。

所以从 Maven 的角度来看,一切都很好!


Eclipse

现在,主要问题来了:拥有良好的 Eclipse 配置。

问题:如何使用 Eclipse 正确管理我当前的配置?特别是,当我使用 Eclipse 在 Tomcat 中部署任何 web-app-X 时...

请注意,我希望获得更自动化的(?)配置,并避免任何手动步骤,因为此配置应该被数十个开发人员使用...

对我来说,最好的解决方案似乎是使用 Eclipse 的链接资源。因此,我在 web-app-X pom.xml 中设置了以下配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <configuration>
                <wtpversion>1.5</wtpversion>
                <linkedResources>
                    <linkedResource>
                        <name>web_resources</name>
                        <type>2</type>
                        <location>${project.basedir}\..\web-resources\src\main\webapp\web_resources</location>
                    </linkedResource>
                </linkedResources>
            </configuration>
        </plugin>
        ...

当我运行 mvn eclipse:eclipse 配置时,它会在我的 web-app-X pom.xml 中成功添加此信息code>.project 文件:

<projectDescription>
  ...
  <linkedResources>
    <link>
      <name>web_resources</name>
      <type>2</type>
      <location>C:\dev\project\web-resources\src\main\webapp\web_resources</location>
    </link>
  </linkedResources>
<projectDescription>

现在,我在 Eclipse 中导入我的项目。 问题:在项目属性>中Java 构建路径 >源,我没有看到链接源。 我只看到四个 Maven 默认目录(src/main/javasrc/main/resourcessrc/test/java>src/test/resources)。 奇怪的是,当我尝试手动添加链接的资源时,它拒绝并说它已经存在......

因此,当我在 Eclipse 中的 Tomcat 上部署 Web 应用程序时,它部署web_resources 目录,因此我没有部署 CSS / JS / 图像。

经过一些测试,似乎我必须做两处修改:

  1. 添加行 在我的 .classpath 文件中;
  2. 删除 .project 文件中的 prefitting-web-resources

请注意,使用此配置,Eclipse 将复制(并保持同步)我的 web-app-X/src/main/webapp/web_resources 中的 web_resources 项目的内容,但这不是问题(这目录被 SCM 忽略)。

我发现的唯一自动化解决方案是创建一个简单的 Maven 插件来执行前面的两个修改,然后运行以下命令(或使用 .bat 文件):

mvn eclipse:clean eclipse:eclipse myEclipsePlugin:eclipse

问题

  • 有没有更好的方法来管理这样的配置?

技术信息

Java 6、Maven 3.0.2、maven eclipse 插件 2.8、Eclipse 3.3.2(但我可以使用较新版本的 Eclipse 进行测试)、 m2eclipse 插件。

Note: I didn't get any response in the first version of this question, so I modified it to be more generic...

Context

My project is divided into several maven modules and several web-applications. Here is the structure:

my-project
  + pom.xml
  +-- commons
  +-- persistence
  +-- ...
  +-- web-app-1
  +-- web-app-2
  +-- ...

All the web applications share common resources, such as JS, CSS and images files.

Instead of duplicating these resources in each web-app-X, I decided to create another project called web-resources, which is a WAR project.
The structure is then the following one:

my-project
  + pom.xml
  +-- commons
  +-- persistence
  +-- ...
  +-- web-app-1
  +-- web-app-2
  +-- ...
  +-- web-resources
       +-- pom.xml
       +-- src/main/webapp
            +-- web.xml (which is almost empty, just need to be present for Maven)
            +-- web_resources
                 +-- css
                 +-- images
                 +-- javascript

Maven

In Maven 2 (or Maven 3, as I just migrated my project to maven 3.0.2), this configuration is easy to manage as all web-app-X declare web-resources as a dependency:

<groupId>foo.bar</groupId>
<artifactId>web-app-1</artifactId>
...
<dependencies>
    <dependency>
        <groupId>foo.bar</groupId>
        <artifactId>web-resources</artifactId>
        <version>${preclosing-version}</version>
        <type>war</type>
    </dependency>
    ...

So when I build my WAR project, it first get the web-resources.war (built just before), unzip it, and build on top of it the web-app-X web-application.
This way, my WAR file will contains also a directory called web-resources/ that contains the shared resources.

This is the war overlay principle.

So on a Maven point of view, everything is fine!


Eclipse

Now, here comes the main problem: having a good Eclipse configuration.

Question: How can I use my current configuration to be managed correctly by Eclipse? In particular, when I deploy any web-app-X in Tomcat using Eclipse...

Note that I want to get the more automatizable (?) configuration, and avoid any manual steps, as this configuration should be used by dozens of developers...

For me, the best solution seems to use the linked resources of Eclipse. Thus, I set the following configuration in my web-app-X pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <configuration>
                <wtpversion>1.5</wtpversion>
                <linkedResources>
                    <linkedResource>
                        <name>web_resources</name>
                        <type>2</type>
                        <location>${project.basedir}\..\web-resources\src\main\webapp\web_resources</location>
                    </linkedResource>
                </linkedResources>
            </configuration>
        </plugin>
        ...

When I run the mvn eclipse:eclipse configuration, it adds succesfully this information in my .project file:

<projectDescription>
  ...
  <linkedResources>
    <link>
      <name>web_resources</name>
      <type>2</type>
      <location>C:\dev\project\web-resources\src\main\webapp\web_resources</location>
    </link>
  </linkedResources>
<projectDescription>

Now, I import my project in Eclipse.
Problem: in Project properties > Java Build Path > Source, I don't see the Link Source present.
I only see my four Maven default directories (src/main/java, src/main/resources, src/test/java and src/test/resources).
What is strange is that when I try to manually add the linked resources, it refuses and says that it already exists...

So when I deploy my web application on my Tomcat in Eclipse, it does not deploy the web_resources directory, and thus I don't get the CSS / JS / images deployed.

After some tests, it seems that I have to do two modifications:

  1. Add the line <classpathentry kind="src" path="web_resources" output="src/main/webapp/web_resources"/> in my .classpath file;
  2. Remove the <project>preclosing-web-resources</project> in the .project file.

Note that using this configuration, Eclipse will copy (and keep synchronization) the content of web_resources project in my web-app-X/src/main/webapp/web_resources, but this is not a problem (this directory is ignored by the SCM).

The only automated solution I found was to create a simple Maven plugin that do the two previous modification, and then run the following command (or use a .bat file):

mvn eclipse:clean eclipse:eclipse myEclipsePlugin:eclipse

Question

  • Is there a better way to manage such configuration?

Technical information

Java 6, Maven 3.0.2, maven eclipse plugin 2.8, Eclipse 3.3.2 (but I can test with newer version of Eclipse), no m2eclipse plugin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-10-23 22:26:55

从 Servlet 3.0 开始,您可以通过将资源放在 src/main/resources/META-INF/resources 目录中来共享资源。

当 web 应用程序部署时,Servlet 3.0 会从上下文路径提供这些资源。例如,在您的情况下...

web-resources
-- src
---- main
------ resources
-------- META-INF
---------- resources
------------ css
-------------- global.css
------------ images
-------------- background.png

假设 my_project 依赖于 web-resources 并部署到 URL http://my.localhost:8080/myProject

通过该配置,以下 URL 将解析为正确的资源:

如果资源与实际应用程序之间存在名称冲突,则应用程序将获胜。

确保您的 web.xml 声明您正在使用 Servlet 3.0

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

Starting with Servlet 3.0, you can share resources by putting them within the src/main/resources/META-INF/resources directory.

When the webapp deploys, Servlet 3.0 makes those resources available from the context path. For example, in your case...

web-resources
-- src
---- main
------ resources
-------- META-INF
---------- resources
------------ css
-------------- global.css
------------ images
-------------- background.png

Let's assume that my_project has a dependency on web-resources and is deployed to the url http://my.localhost:8080/myProject.

With that configuration, the following URLs will resolve to the correct resources:

IF you have a name conflict between the resources and your actual application, the application will win.

Be sure your web.xml states you are using Servlet 3.0

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
银河中√捞星星 2024-10-23 22:26:55

您可能想看看这篇博文。它涵盖了使用maven共享资源的方法:

http://www.sonatype.com/people/2008/04/how-to-share-resources-across-projects-in-maven/

You might want to take a look at this blog post. It covers a method for sharing resources using maven:

http://www.sonatype.com/people/2008/04/how-to-share-resources-across-projects-in-maven/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文