SpringMVC projects use maven to hit executable jar packages to reference local dependencies

          In the past two days, the maven project needs to be packaged into an executable jar package, and it needs to depend on the local project jar package, and then various problems occur. After maven jar package, various local dependent packages cannot be found; using java packaging and Eclipse export can After the jar package is executed, the class files in the jar package cannot find the path. After 2 days of exhaustion, it can be executed normally. Without further ado, let’s just explain:

pom.xml key configuration:

first step:

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-shade-plugin</artifactId>
			<version>2.4.1</version>
			<executions>
				<execution>
					<phase>package</phase>
					<goals>
						<goal>shade</goal>
					</goals>
					<configuration>
						<transformers>
							<!-- Configure the main program startup entry-->
							<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
								<mainClass>cc.eslink.etbc.StandaloneBoot</mainClass>
							</transformer>
							<!--Multiple jar packages of Spring Framework contain the same files spring.handlers and spring.schemas. If a single jar package is generated, they will overwrite each other.
							In order to avoid mutual influence, AppendingTransformer can be used to append and merge the contents of the file -->
							<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
								<resource>META-INF/spring.handlers</resource>
							</transformer>
							<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
								<resource>META-INF/spring.schemas</resource>
							</transformer>
						</transformers>
					</configuration>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

 

  The above code can refer to the source description

http://xxgblog.com/2015/08/07/maven-create-executable-jar/

    The Class-Path content is not generated in MANIFEST.MF after the above jar is typed, and the quick way to obtain the content will be written below.

After the package is successfully packaged using the mvn package command, a jar package will be produced in the target directory . This jar package is temporarily named package A.

Since there are local dependency packages used in the project, there is an error that the file cannot be found after execution. Even if the local package is entered into the jar package, it cannot be read. I have tried to enter the jar package together with the dependent package. In the jar package, and then configure the calsspath in MANIFEST.MF, it still doesn't work. It is estimated that my configuration method is wrong, but I don't care.

 

Step 2: Concatenate all dependent package names in class-path

The method uses eclipse to export the jar package

Right-click on the project name-export-java-Runnable JAR file, click next, launch configuration select your startup main method, export des select your export path, (I chose the second package in the library...) Click finish, Export the jar package, here named B package, after success,

Find the exported B jar package, open it, find MANIFEST.MF, open the Rerc-Class-Path in the copy (the class-path I generated locally is not worth it, j), and copy it to the MANIFEST.MF Class-Path in the A jar package , you can.

Modify the MANIFEST.MF file in the execution jar
original file content

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: dfsoft-ZYJ
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_60
Main-Class: cc.xiaobin.etbc.StandaloneBoot //Program running entry

After adding Class-Path:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: dfsoft-ZYJ
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_60
Class-Path:  activation-1.1.jar alipay-sdk-java20151210120052.j
 ar alipay.jar antlr-2.7.7.jar aopalliance-1.0.jar
Main-Class: cc.xiaobin.etbc.StandaloneBoot //Program running entry

Separate multiple jar packages with spaces

 

Step 3: Put the local dependency jar package

Put the local dependency jar package in the same directory as the executable A jar package,

The executable A jar package is currently in the test directory, and then copy all the local dependent jar packages to the test directory.

 

Step 4: Execute the jar package

Use [ java –jar jarName.jar parameter 1, parameter 2 ... ] to execute the jar package

 

So far, my jar package can finally be executed normally, and there is no need to report path exceptions, files cannot be found, and xml cannot parse these problems. It was only after I finished it that I found out that it was so simple. The main reason that bothered me was the strange problem of not finding the file in the early stage. The naming has been entered into the jar package or there is still an error, which has caused me to fix it for a long time. It turns out that the jar package that depends on the jar package needs to be at the same level as himself to be normal.

After exporting the executable jar package using java or eclipse , because the code for obtaining the file path is used in the project, various paths are reported incorrectly in the project operation, and the maven package test will not be performed. So I did not use the java method. If the gods have a better way, please leave a message to let me learn, thank you.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326453845&siteId=291194637