gpt4 book ai didi

java - 使用 Maven 将 Java 模块依赖资源合并到 WAR 中

转载 作者:行者123 更新时间:2023-12-02 05:57:54 26 4
gpt4 key购买 nike

我有一个多平台项目,其中有多个特定于语言的配置文件。 HTML 平台特定模块依赖于“assets-core”模块,如果启用“en”配置文件,则“assets-en”模块资源也应包含在 WAR 中。

组装 WAR 工作得很好,但有一个异常(exception),如果“assets-core”模块包含与“assets-en”同名的文件,则它不会被“assets-en”模块中的文件覆盖。

我尝试使用maven-resource-plugin:

  <build>
<resources>
<resource>
<directory>${basedir}/assets-en/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>

  <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-prod-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/assets-ar/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

true 按照其他线程的建议启用,但没有任何效果。

maven-war-plugin 覆盖也可能是解决方案,但我无法将资源模块与 war 一起打包,因为 Java 项目中也包含相同的模块。

以下是我的 pom.xml 的相关部分:

asssets-core/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>example-parent</artifactId>
<version>1.0</version>
</parent>

<artifactId>assets-core</artifactId>
</project>

assets-en/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>example-parent</artifactId>
<version>1.0</version>
</parent>

<artifactId>assets-en</artifactId>
</project>

示例-html/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>example-parent</artifactId>
<version>1.0</version>
</parent>

<artifactId>example-html</artifactId>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-core</artifactId>
<version>${project.version}</version>
<classifier>sources</classifier>
</dependency>

<dependency>
<groupId>com.example</groupId>
<artifactId>assets-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${gwt.version}</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<profiles>
<profile>
<id>en</id>

<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>assets-en</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>

最佳答案

好的,我终于找到解决办法了。您需要摆脱对模块的依赖并使用 maven-resources-plugin 复制资源:

示例-html/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>example-parent</artifactId>
<version>1.0</version>
</parent>

<artifactId>example-html</artifactId>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-core</artifactId>
<version>${project.version}</version>
<classifier>sources</classifier>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${gwt.version}</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-assets-core</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/ExampleProject/</outputDirectory>
<resources>
<resource>
<directory>${project.parent.basedir}/assets-core/src/main/resources/assets</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<profiles>
<profile>
<id>en</id>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-assets-en</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/ExampleProject/</outputDirectory>
<resources>
<resource>
<directory>${project.parent.basedir}/assets-en/src/main/resources/assets</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

关于java - 使用 Maven 将 Java 模块依赖资源合并到 WAR 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22910268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com