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

。_饼干妹妹 提交于 2019-12-08 11:35:33

问题


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>

回答1:


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



来源:https://stackoverflow.com/questions/32296811/maven-jar-with-dependencies-without-target-classes-build-artifacts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!