【问题标题】:Maven module for static files用于静态文件的 Maven 模块
【发布时间】:2016-03-16 13:27:27
【问题描述】:

我有一个带有 spring 和 maven 的 Web 应用程序。该应用程序分为 Maven 模块。我有一个生成战争的“应用程序网络”模块。另一个生成耳朵的“应用程序耳朵”模块。还有一个带有 css、js 和图像的“静态应用程序”,可以生成一个 zip 文件。

这将是我的申请大纲:

  • 申请
    • 应用程序-web(java 代码)
    • 应用耳
    • 应用程序静态(css、js 和图像)
    • 应用程序资源(语言属性)

我想部署“静态”模块的eclipse使用文件,而不是使用“application/src/main/webapp”目录中的文件。我怎样才能做到这一点?有可能吗?

【问题讨论】:

  • 为什么要把css/js和图片移到application-static而不是直接移到application-web模块?
  • 这些是项目的规格。一个用于静态文件的模块,一个用于国际化消息,另一个用于配置文件设置。
  • 这可行,但我还有另一个问题。

标签: java eclipse spring maven maven-assembly-plugin


【解决方案1】:

您可以使用 maven 依赖插件解压缩包含在 zip 文件中的静态资源,使用以下方法:

在你的 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

【讨论】:

  • 我已经这样做了。我有 zip 文件。但我不知道如何将 zip 文件放入应用程序网络中以使用它。这是我第一次做这样的事情。
  • 我明白你的意思。您可以使用上面提到的 maven-dependency 插件
  • 谢谢,看起来是个不错的解决方案。明天我试一试并讨论结果
  • @oscar 不是,看我的回答。
【解决方案2】:

这项工作:

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

但是我还有另一个问题,这个解决方案只在我执行 clean package 时复制静态文件。如果我使用 Eclipse 运行应用程序 Web,并且我在应用程序静态中的 js 文件中进行了更改,那么在我执行 clean package 之前,这些更改将无效。有什么解决办法吗?

【讨论】:

    【解决方案3】:

    不要浪费时间拆包资源,一切都是开箱即用的:

    1. 资源包:始终从类路径加载,无论是展开 (WEB-INF/classes) 还是压缩 (WEB-INF/lib)
    2. 提供静态资源:将它们捆绑在一个 JAR 中并使用 Tomcat (Servlet 3.0) 功能从 META-INF/resources 提供服务,请参阅 here 或使用 Spring 内置的 mvc:resources 元素。

    只需在 WAR POM 中添加依赖项 sn-ps 即可完成。

    【讨论】:

    • 我在 application-web pom 中有 application-static 依赖,并删除了 de build 部分。我删除了 src/main/webapp 目录的静态文件。如何使用 mvc: 资源获取静态文件?在我使用 之前,因为文件位于 application-web 模块中。
    • @oscar location 正在使用 Spring 中应用的默认资源路径。即,由于您在 webapp 中,默认位置是针对 webapp 解析的,要从 JAR 获取资源,请使用 classpath:/assets/
    • 不适合我,应用程序静态模块生成一个 zip 而不是 jar。在eclipse中部署找不到静态文件。在 application-web war 中找不到 zip 或静态文件。
    • @oscar 使用标准的 JAR 就可以了。在这种环境下使用 ZIP 文件没有任何意义。
    • 如果使用jar文件,文件(application-static.jar)被复制到WEB-INF/lib目录但找不到静态文件。感谢您的耐心,我是 maven 和打包模块的菜鸟。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2019-04-13
    • 2017-09-16
    相关资源
    最近更新 更多