如何在Maven和Eclipse中管理多个Web应用程序的共享资源? [英] How to manage shared resources for several web apps in Maven AND Eclipse?

查看:148
本文介绍了如何在Maven和Eclipse中管理多个Web应用程序的共享资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



上下文



我的项目分为几个maven模块和几个Web应用程序。这是结构:

  my-project 
+ pom.xml
+ - commons
+ - 持久性
+ - ...
+ - web-app-1
+ - web-app-2
+ - ...

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



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

  my-project 
+ pom.xml
+ - commons
+ - 持久性
+ - ...
+ - web-app-1
+ - web-app-2
+ - ...
+ - web资源
+ - pom.xml
+ - src / main / webapp
+ - web.xml(这几乎是空的,只需要出现在Maven)
+ - web_resources
+ - css
+ - 图像
+ - javascript






Maven



在Maven 2(或Maven 3,因为我刚刚迁移我的项目到maven 3.0.2),这个配置很容易管理,因为所有 web-app-X 声明 web资源作为依赖:

 <&的groupId GT; foo.bar< /&的groupId GT; 
< artifactId> web-app-1< / artifactId>
...
<依赖关系>
<依赖关系>
< groupId> foo.bar< / groupId>
< artifactId> web资源< / artifactId>
< version> $ {preclosing-version}< / version>
< type> war< / type>
< / dependency>
...

所以当我建立我的WAR项目时,首先得到 web-resources.war (刚刚构建),解压缩,并在其上构建 web-app-X web-application。
这样,我的WAR文件还将包含一个名为 web-resources / 的包含共享资源的目录。



这是战争叠加原则。



所以在Maven的观点,一切都很好!






Eclipse



现在,主要问题是:



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



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



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

 code><建立> 
< 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 配置,它在我的 .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 / java src / main / resources src / test / java src / test / resources )。
什么是奇怪的是,当我尝试手动添加链接的资源,它拒绝并说它已经存在...



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



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


  1. 在我的<$ c $中添加行< classpathentry kind =srcpath =web_resourcesoutput =src / main / webapp / web_resources/> c> .classpath file;

  2. 删除< project> preclosing-web-resources< / project> 文件。

请注意,使用此配置, 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插件。

解决方案

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



当webapp部署时,Servlet 3.0会使这些资源从上下文路径中可用。例如,在你的情况下...

  web-resources 
- src
--- - 主
------资源
-------- META-INF
----------资源
--- --------- css
-------------- global.css
------------图像
-------------- background.png

让我们假设 my_project 依赖于网络资源,并被部署到网址 http:/ /my.localhost:8080/myProject



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





如果 r之间的冲突资源和您的实际应用程序,应用程序将获胜。



确保您的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>


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.

解决方案

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">

这篇关于如何在Maven和Eclipse中管理多个Web应用程序的共享资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆