Java fatJar with resources and dependencies for testNG

[亡魂溺海] 提交于 2021-01-29 05:41:58

问题


I'm trying to build a testng fatjar but looks like this configuration is not adding the resources from java main and tests

   <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <argLine>--enable-preview</argLine>
                <descriptor>src/main/assembly/assembly.xml</descriptor>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>Main</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>

And here is the config assembly.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>fat-tests</id>
<formats>
    <format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <useProjectArtifact>true</useProjectArtifact>
        <unpack>true</unpack>
        <scope>test</scope>
    </dependencySet>
</dependencySets>
<fileSets>
    <fileSet>
        <directory>${project.build.directory}/test-classes</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>**/*.class</include>
        </includes>
        <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
    <fileSet>
        <directory>${project.build.directory}/test-classes</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>**/*.xml</include>
        </includes>
        <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
</fileSets>

I have tried different goals and configs but can not find how to add all folders to the jar, resources from java, and test folder.


回答1:


To copy all the resources from target/classes and target/test-classes folder try below file. All the resources will be copied to root folder in jar file.

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>fat-tests</id>
<formats>
    <format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <useProjectArtifact>true</useProjectArtifact>
        <unpack>true</unpack>
        <scope>test</scope>
    </dependencySet>
</dependencySets>
<fileSets>
<fileSet>
        <directory>${project.build.directory}/classes</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>**/*.*</include>
        </includes>
        <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
    <fileSet>
        <directory>${project.build.directory}/test-classes</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>**/*.class</include>
        </includes>
        <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
    <fileSet>
        <directory>${project.build.directory}/test-classes</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>**/*.*</include>
        </includes>
        <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
</fileSets>


来源:https://stackoverflow.com/questions/64945566/java-fatjar-with-resources-and-dependencies-for-testng

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