Matoy
Matoy

Reputation: 1808

Maven jar-with-dependencies without target\classes (build artifacts)

I want to create an executable jar (with all the *.class of my code in it). However, I don't want the Jar to include the resources that during compilation are in my src/main/resources path.

my project hierarchy is:

project
  -src
     -main

    -resources
        -resources_setting.xml
  -target
     -classes
       -resources_setting.xml

I want my jar to include only the classes of main and the dependencies, not the resources inside target\classes or inside resources.

How do I do that?

I am using maven-assembly-plugin, like this:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>cqm.qa.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Upvotes: 0

Views: 2168

Answers (1)

RahulRP
RahulRP

Reputation: 563

For bundling purpose, I usually used the maven-shade-plugin and the settings are described below. It will work same as assembly plugin.

 <profile>
    <id>generate-shaded-jar</id>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                 <excludes>
                   <exclude>**</exclude>
                 </excludes>
           </resource>
        </resources>
        <plugins>           
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                       <executions>
                           <execution>
                               <phase>package</phase>
                               <goals>
                                   <goal>shade</goal>
                               </goals>
                               <configuration>
                                   <transformers>
                                       <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                            <manifestEntries>
                                                <Main-Class>cqm.qa.Main</Main-Class>
                                                <Class-Path>.</Class-Path>
                                             </manifestEntries>
                                        </transformer>
                                    </transformers>
                                   <filters>
                                        <filter>
                                            <artifact>*:*</artifact>
                                             <excludes>
                                                <exclude>log4j.properties</exclude>
                                                <exclude>details.properties</exclude>
                                              </excludes>
                                         </filter>
                                    </filters>
                                </configuration>
                            </execution>
                        </executions>
                <configuration>
                    <finalName>cqm-full</finalName>
                </configuration>
            </plugin> 
        </plugins>
    </build>
</profile>

In the above configuration, I've excluded log4j.properties and details.properties from the final jar with dependencies with name cqm-full.jar

Update

Invoke the profile using mvn install -Pgenerate-shaded-jar

Now resource files from src/main/resources won't get added in cqm-full.jar. If invoked without profile mvn clean install, you can still view the resources in the jar

Upvotes: 4

Related Questions