简体   繁体   English

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

[英]How can I include resources (css, js) from jar using maven assembly?

I have a project which contains shared resources for several sub-projects; 我有一个项目,其中包含多个子项目的共享资源; ie as well as Java code, it contains css and javascript. 即,以及Java代码,它包含CSS和JavaScript。

My child sub-projects are packaged as jars using the maven assembly plugin. 我的子项目使用maven程序集插件打包为jar。 For some reason these do not include the css and js resources from the parent library. 由于某些原因,这些不包括父库中的css和js资源。

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. 它们是作为独立jar运行的dropwizard(/ jetty)项目。

EDIT - I should make explicit that the resources are included in the parent project's jar; 编辑 - 我应该明确表示资源包含在父项目的jar中; it's only when assembly (on a child project) includes the jar that they somehow go missing. 只有当汇编(在子项目中)包含他们以某种方式丢失的jar时。

Parent pom: 父母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>

Example child pom: 示例儿童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.> 它们是作为独立jar运行的dropwizard(/ jetty)项目

You need to add resource directory in <resource></resource> so that it picks up while building jar 您需要在<resource></resource>添加资源目录,以便在构建jar时获取它

You could either use maven-assembly plugin or configure your pom.xml to have these resources files specified as maven resources, assembly plugin will give you much more flexibility 您可以使用maven-assembly插件或配置pom.xml以将这些资源文件指定为maven资源, 程序集插件将为您提供更多灵活性

I ended up using maven shade instead. 我最后使用了maven shade。 It's a little more heavyweight than assembly but it does the job. 它比装配重一点,但是它可以完成任务。

Still, if anybody knows how to make assembly do this please add an answer. 不过,如果有人知道如何组装,请添加答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM