gpt4 book ai didi

java - 如何使用 maven 程序集从 jar 中包含资源(css,js)?

转载 作者:搜寻专家 更新时间:2023-11-01 00:56:48 24 4
gpt4 key购买 nike

我有一个项目,其中包含多个子项目的共享资源;即除了 Java 代码外,它还包含 css 和 javascript。

我的子项目使用 maven 程序集插件打包为 jar。由于某些原因,这些不包括父库中的 css 和 js 资源。

所以我的问题很简单:如何配置项目才能实现这种情况?我不希望子项目成为 war ;它们是作为独立 jar 运行的 dropwizard (/jetty) 项目。

编辑 - 我应该明确指出资源包含在父项目的 jar 中;只有当程序集(在子项目上)包含它们以某种方式丢失的 jar 时。

父pom:

<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.parentproject</groupId>
<artifactId>parentproject</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
...
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.gwt.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-proc:none</compilerArgument>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
...
</plugins>
</build>
</project>

示例子 pom:

<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.child</groupId>
<artifactId>child</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Child</name>

<dependencies>
<dependency>
<groupId>io.parentproject</groupId>
<artifactId>parentproject</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
...
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-proc:none</compilerArgument>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>io.childproject.SomeClass</mainClass>
</manifest>
</archive>
<finalName>${project.artifactId}-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>

</project>

最佳答案

So my question is simple: how can I configure the projects so that this happens? I don't want the child projects to be wars; they are dropwizard (/jetty) projects that run as standalone jars.>

需要在<resource></resource>中添加资源目录以便它在构建 jar 时拾取

您可以使用 maven-assembly 插件或配置您的 pom.xml 以将这些资源文件指定为 maven 资源,assembly plugin会给你更多的灵 active

关于java - 如何使用 maven 程序集从 jar 中包含资源(css,js)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21394787/

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