【问题标题】:Maven dependency on a web applicationMaven 对 Web 应用程序的依赖
【发布时间】:2011-09-25 19:23:02
【问题描述】:

刚想到maven的依赖系统,就问自己,有没有可能依赖web应用(*.war文件)? 我想将应用程序的核心模块维护为普通的 Web 应用程序。更具体的实现将使用核心作为依赖并对其进行扩展。

例如,我有以下内容:

  1. Core.war // 后端
  2. ConcreteProject1.war // 客户 1 的前端
  3. ConcreteProject2.war // 客户 2 的前端

具体项目现在应该依赖于 Core.war 并包含所有网页、其他资源、源文件等,但如果在上下文路径中存在同名文件,具体项目应该覆盖原来的。如果可以合并这些文件,那就更好了!想想例如一个日志配置文件,它将与自定义文件合并,以便为客户端进行特殊处理。还有许多其他用例会非常有用。

谁能给我一个提示,告诉我我可以做些什么来在我的构建过程中得到这样的行为?

【问题讨论】:

  • 我才意识到它确实有效!我现在已经测试了依赖关系,但是是否也可以为某些文件定义诸如合并之类的东西? database.properties 只需要用户名、密码等,但不是所有配置。

标签: web-applications maven dependencies


【解决方案1】:

你需要一些类似的东西......

假设您有两个模块 - core 和 web-module1,并且您需要在最后的战争中组装它们。

核心的 POM:

<project>

    <groupId>com.foo</groupId>
    <artifactId>core</artifactId>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archiveClasses>false</archiveClasses>
                    <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>

                    <webappDirectory>${project.build.directory}/mywebapp</webappDirectory>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

依赖于核心的模块的POM:

<project>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.foo</groupId>
    <artifactId>web-module1</artifactId>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>

                    <overlays>
                        <overlay>
                            <excludes>
                                <exclude>**/web.xml</exclude>
                            </excludes>
                        </overlay>
                    </overlays>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>core</artifactId>
            <version>${project.version}</version>
            <scope>provided</scope>
            <type>war</type>
        </dependency>
    </dependencies>

</project>

大会:

<project>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.foo</groupId>
    <artifactId>web-assembly</artifactId>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archiveClasses>false</archiveClasses>
                    <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>

                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                            <includes>
                                <include>*.xml</include>
                            </includes>
                            <targetPath>WEB-INF</targetPath>
                        </resource>
                    </webResources>

                    <webappDirectory>${project.build.directory}/mywebapp</webappDirectory>
                    <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>

                    <overlays>
                        <overlay>
                            <excludes>
                                <exclude>**/web.xml</exclude>
                            </excludes>
                        </overlay>
                    </overlays>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>core</artifactId>
            <version>${project.version}</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>com.foo</groupId>
            <artifactId>web-module1</artifactId>
            <version>${project.version}</version>
            <type>war</type>
        </dependency>
    </dependencies>

</project>

【讨论】:

  • 谢谢,看起来真不错。是否还有合并文件的方法,例如配置等?
  • 嗯,有一些方法,但我避免这样做。据我所知,您可以使用 cargo 插件,或类似的东西。我通常做的是我拥有相应模块中的资源,它们只涉及模块的设置。在程序集中我有完整的设置。例如对于 web.xml。
猜你喜欢
  • 2014-09-10
  • 1970-01-01
  • 2016-06-24
  • 2013-04-29
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 2015-06-20
  • 2021-07-07
相关资源
最近更新 更多