Combine multiple jar into one (using maven)

2019-07-04 06:11发布

问题:

I have a project in javafx , I have 3 dependencies I try to combine them with my principale jar using maven : The result I got a jar (1.82mb) but when I click he dosen't launch noting appears.

pom.xml

<?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>
    <dependencies>
        <dependency>
            <groupId>org.scilab.forge</groupId>
            <artifactId>jlatexmath</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>fxgraphics2d</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.controlsfx</groupId>
            <artifactId>controlsfx</artifactId>
            <version>8.0.6_20</version>
        </dependency>
    </dependencies>
    <groupId>groupId</groupId>
    <artifactId>FXCalc</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>

and this is some screenshot :

The problem : the jar file I got dosen't work , it dosen't want to launch , I tried using ant and gradle but I don't know how to use them.

EDIT : After trying the solution I get a jar but it still dosen't launch and I have no error. EDIT 2 :

<?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>
    <dependencies>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>fxgraphics2d</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.scilab.forge</groupId>
            <artifactId>jlatexmath</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.controlsfx</groupId>
            <artifactId>controlsfx</artifactId>
            <version>8.0.6</version>
        </dependency>
    </dependencies>
    <build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>sample.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    </build>
    <groupId>groupId</groupId>
    <artifactId>CalculatorFX</artifactId>
    <version>1.0-SNAPSHOT</version>


</project>

回答1:

Add the following plugin to your pom.xml after dependencies:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>sampler.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Then run the mvn package command from your base directory, where your project is. This will generate a single jar in the target folder.