Maven package dependency lib is separated from program jar

Maven package dependency lib is separated from program jar

Package configuration

	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <!-- 添加classpath缺少的内容-->
                        <manifestEntries>
                            <!--这里是打包后的lib文件夹,不是本项目下的lib文件夹,
                            如果上面(dependency标签)没有引入项目下的lib-jar包,maven打包后需要把项目lib下的jar复制到maven打包后的lib,
                            并在下方<Class-Path>引入,否则程序运行时在不到jar依赖-->
<!--                            <Class-Path>lib/bcprov-1.0.jar lib/jacob-1.0.jar lib/jave-1.0.2-1.0.jar</Class-Path>-->
                            <Class-Path>lib/jave-1.0.2-1.0.jar</Class-Path>
                        </manifestEntries>
                        <!-- 在生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifest>
                            <!-- 第三方jar加入到类构建路径 -->
                            <addClasspath>true</addClasspath>
                            <!-- 依赖jar包的最终位置 -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.yu.DemoApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!--拷贝依赖到lib目录-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-lib</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib</outputDirectory>
                            <excludeTransitive>false</excludeTransitive>
                            <stripVersion>false</stripVersion>
                            <includeScope>compile</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

View

Insert picture description here
Unzip the jar package: demo-1.3.1
Insert picture description here
Check whether the Class-Path is added OK: META-INF --> MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: demo
Implementation-Version: 1.3.1
Class-Path: lib/jave-1.0.2-1.0.jar lib/rxtxcomm-2.2.jar lib/thumbnaila
 tor-0.4.8.jar lib/jacob-1.14.3.jar lib/spring-boot-starter-websocket-
 2.2.12.RELEASE.jar lib/spring-messaging-5.2.12.RELEASE.jar
 ----------省略-------------
 lib/jackson-mapper-asl-1.9.13.jar
Build-Jdk-Spec: 1.8
Created-By: Maven Archiver 3.4.0
Main-Class: com.yu.DemoApplication

lib/jave-1.0.2-1.0.jar lib/rxtxcomm-2.2.jar has been added, it will be ok after the project is started

run

Script running reference address
java -Dloader.path=lib/ -jar demo-1.3.1.jar

k@kong:/kong/demo/target$ java -Dloader.path=lib/ -jar tcmsys-1.3.1.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.2.12.RELEASE)

2021-01-12 10:06:54.326  INFO 30850 --- [           main] c.yu.DemoApplication  : Starting DemoApplication v1.3.1 on kong with PID 30850 (/kong/demo/target/demo-1.3.1.jar started by hdas in /kong/demo/target)
2021-01-12 10:06:54.328  INFO 30850 --- [           main] c.yu.demo.DemoApplication  : No active profile set, falling back to default profiles: default
2021-01-12 10:06:55.371  INFO 30850 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 6143 (http)
2021-01-12 10:06:55.382  INFO 30850 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-01-12 10:06:55.383  INFO 30850 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-01-12 10:06:55.432  INFO 30850 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-01-12 10:06:55.433  INFO 30850 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1049 ms
2021-01-12 10:06:55.832  INFO 30850 --- [           main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation(String, HttpServletRequest)]
2021-01-12 10:06:55.926  INFO 30850 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-01-12 10:06:56.019  WARN 30850 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2021-01-12 10:06:56.157  INFO 30850 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2021-01-12 10:06:56.175  INFO 30850 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2021-01-12 10:06:56.201  INFO 30850 --- [           main] s.d.s.w.s.ApiListingReferenceScanner     : Scanning for api listing references
2021-01-12 10:06:56.431  INFO 30850 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 6143 (http) with context path ''
2021-01-12 10:06:56.433  INFO 30850 --- [           main] c.yu.demo.DemoApplication  : Started TcmofflineApplication in 2.368 seconds (JVM running for 2.612)

Guess you like

Origin blog.csdn.net/qq_42476834/article/details/112507565