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

标签 java maven maven-assembly-plugin

我有一个项目,其中包含多个子项目的共享资源;即除了 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/

相关文章:

java - 将 Canvas 的特定区域设置为可点击

eclipse - JavaServer Faces 2.2 需要 Dynamic Web Module 2.5 或更高版本

maven-2 - 使用 Maven 创建一个 zip 文件,其中包含具有依赖关系的可执行 jar

java - Maven测试执行错误

java - 使用带有 JOGL 的 maven assembly 插件创建可执行 jar

maven-2 - Maven2 程序集插件 - 更改 tar.gz 的根名称

java - 使用 log4j 生成日志文件的问题

java - Java中如何为类成员函数分配内存

java - 当类中没有定义泛型类型变量时处理泛型类成员

java - 使用 WebAppCreator 制作的 GWT Maven 项目在开发模式下不工作