Question

I have a project which contains shared resources for several sub-projects; i.e. as well as Java code, it contains css and javascript.

My child sub-projects are packaged as jars using the maven assembly plugin. For some reason these do not include the css and js resources from the parent library.

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.

EDIT - I should make explicit that the resources are included in the parent project's jar; it's only when assembly (on a child project) includes the jar that they somehow go missing.

Parent 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:

<?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>
Was it helpful?

Solution 2

I ended up using maven shade instead. 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.

OTHER TIPS

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.>

You need to add resource directory in <resource></resource> so that it picks up while building 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top