使用Spring Boot 2和Maven 3将外部jar包含在打包中
首先
因为现场团队成员陷入困境,即使我也试图研究并尝试他们的方法,但仍然无法成功,所以我单独查询了相关信息,并做了备忘录。
这个内容不是特别难,但一旦陷入其中,就很容易陷进去。
环境
Java8
Java8 is a programming language and computing platform that is commonly used for developing and running various applications. It provides a wide range of features and improvements compared to previous versions of Java.
Maven3
Maven3 is a build automation tool used primarily for Java projects. It helps developers manage project dependencies, compile source code, run tests, and create executable files or libraries. Maven3 simplifies the software development process by providing a standard way to build and manage projects.
Spring Boot 2.0.2.RELEASE
Spring Boot 2.0.2.RELEASE is a framework based on the Spring framework. It aims to simplify the development of Java applications by providing a set of boilerplate configurations and commonly used functionalities. It helps developers create standalone, production-grade applications with minimal time and effort.
为了什么
将包含外部jar的jar文件与通过./mvnw clean package命令创建的jar文件合并。
创建项目
这次只需要将其包含在打包中即可,所以没有特别的实现。
pom.xml 文件
我们将在pom.xml中进行外部jar的配置,而不需要进行任何实现。
依赖关系
为了避免仅限于package而显得无用,我会将这次想要添加的jar文件的信息添加到依赖项中。
添加后进行更新,应该就不会在Eclipse开发环境中出现困难了。
我对于Maven的变量展开并不是非常详细了解,但在设置文件夹时,常常会搜索到${project.basedir}和${basedir}这两个作为指定基准文件夹的选项。据说前者指向项目目录,后者指向pom.xml文件所在的目录。根据我在Maven页面上的了解,我会使用${project.basedir}作为基准文件夹。
请确保版本号和jar文件名的最后一个版本号是一致的,因为似乎需要这样(否则在Eclipse中会出现错误),请将它们保持一致。
<dependency>
<groupId>com.tasogarei</groupId>
<artifactId>hogehoge</artifactId>
<version>1</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/hogehoge1.jar</systemPath>
</dependency>
插件
可以通过插件的设置将其混合到jar中。
只需要在配置中设置includeSystemScope为true即可。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
将jar文件放置在设定目录中。
由于这次设置了${project.basedir}/lib/hogehoge1.jar,所以我们将放置一个合适的jar文件。
由于没有特别要求,所以我们随便从.m2/目录中选择了一个jar文件。
确保里面是真的
以上是设置内容。我们来试试看。
请提供中文翻译的参考文本。
使用mvnw命令将其转换为jar文件。
./mvnw clean package
检查罐子里的内容
由于jar文件创建成功,我将使用jar tf来确认所创建的jar文件的内容。
确认后发现BOOT-INF/lib/hogehoge-1.jar存在,因此我成功地将外部jar文件包含在内。
$ jar tf jartest-0.0.1-SNAPSHOT.jar | sort
BOOT-INF/
BOOT-INF/classes/
BOOT-INF/classes/application.properties
BOOT-INF/classes/com/
BOOT-INF/classes/com/tasogarei/
BOOT-INF/classes/com/tasogarei/app/
BOOT-INF/classes/com/tasogarei/app/JartestApplication.class
BOOT-INF/lib/
BOOT-INF/lib/hogehoge-1.jar
BOOT-INF/lib/javax.annotation-api-1.3.2.jar
BOOT-INF/lib/jul-to-slf4j-1.7.25.jar
BOOT-INF/lib/log4j-api-2.10.0.jar
BOOT-INF/lib/log4j-to-slf4j-2.10.0.jar
BOOT-INF/lib/logback-classic-1.2.3.jar
BOOT-INF/lib/logback-core-1.2.3.jar
BOOT-INF/lib/slf4j-api-1.7.25.jar
BOOT-INF/lib/snakeyaml-1.19.jar
BOOT-INF/lib/spring-aop-5.0.6.RELEASE.jar
BOOT-INF/lib/spring-beans-5.0.6.RELEASE.jar
BOOT-INF/lib/spring-boot-2.0.2.RELEASE.jar
BOOT-INF/lib/spring-boot-autoconfigure-2.0.2.RELEASE.jar
BOOT-INF/lib/spring-boot-starter-2.0.2.RELEASE.jar
BOOT-INF/lib/spring-boot-starter-logging-2.0.2.RELEASE.jar
BOOT-INF/lib/spring-context-5.0.6.RELEASE.jar
BOOT-INF/lib/spring-core-5.0.6.RELEASE.jar
BOOT-INF/lib/spring-expression-5.0.6.RELEASE.jar
BOOT-INF/lib/spring-jcl-5.0.6.RELEASE.jar
META-INF/
META-INF/MANIFEST.MF
META-INF/maven/
META-INF/maven/com.tasogarei/
META-INF/maven/com.tasogarei/jartest/
META-INF/maven/com.tasogarei/jartest/pom.properties
META-INF/maven/com.tasogarei/jartest/pom.xml
org/
org/springframework/
org/springframework/boot/
org/springframework/boot/loader/
org/springframework/boot/loader/archive/
org/springframework/boot/loader/archive/Archive$Entry.class
org/springframework/boot/loader/archive/Archive$EntryFilter.class
org/springframework/boot/loader/archive/Archive.class
org/springframework/boot/loader/archive/ExplodedArchive$1.class
org/springframework/boot/loader/archive/ExplodedArchive$FileEntry.class
org/springframework/boot/loader/archive/ExplodedArchive$FileEntryIterator$EntryComparator.class
org/springframework/boot/loader/archive/ExplodedArchive$FileEntryIterator.class
org/springframework/boot/loader/archive/ExplodedArchive.class
org/springframework/boot/loader/archive/JarFileArchive$EntryIterator.class
org/springframework/boot/loader/archive/JarFileArchive$JarFileEntry.class
org/springframework/boot/loader/archive/JarFileArchive.class
org/springframework/boot/loader/data/
org/springframework/boot/loader/data/RandomAccessData.class
org/springframework/boot/loader/data/RandomAccessDataFile$1.class
org/springframework/boot/loader/data/RandomAccessDataFile$DataInputStream.class
org/springframework/boot/loader/data/RandomAccessDataFile$FileAccess.class
org/springframework/boot/loader/data/RandomAccessDataFile.class
org/springframework/boot/loader/ExecutableArchiveLauncher.class
org/springframework/boot/loader/jar/
org/springframework/boot/loader/jar/AsciiBytes.class
org/springframework/boot/loader/jar/Bytes.class
org/springframework/boot/loader/jar/CentralDirectoryEndRecord.class
org/springframework/boot/loader/jar/CentralDirectoryFileHeader.class
org/springframework/boot/loader/jar/CentralDirectoryParser.class
org/springframework/boot/loader/jar/CentralDirectoryVisitor.class
org/springframework/boot/loader/jar/FileHeader.class
org/springframework/boot/loader/jar/Handler.class
org/springframework/boot/loader/jar/JarEntry.class
org/springframework/boot/loader/jar/JarEntryFilter.class
org/springframework/boot/loader/jar/JarFile$1.class
org/springframework/boot/loader/jar/JarFile$2.class
org/springframework/boot/loader/jar/JarFile$JarFileType.class
org/springframework/boot/loader/jar/JarFile.class
org/springframework/boot/loader/jar/JarFileEntries$1.class
org/springframework/boot/loader/jar/JarFileEntries$EntryIterator.class
org/springframework/boot/loader/jar/JarFileEntries.class
org/springframework/boot/loader/jar/JarURLConnection$1.class
org/springframework/boot/loader/jar/JarURLConnection$JarEntryName.class
org/springframework/boot/loader/jar/JarURLConnection.class
org/springframework/boot/loader/jar/StringSequence.class
org/springframework/boot/loader/jar/ZipInflaterInputStream.class
org/springframework/boot/loader/JarLauncher.class
org/springframework/boot/loader/LaunchedURLClassLoader$UseFastConnectionExceptionsEnumeration.class
org/springframework/boot/loader/LaunchedURLClassLoader.class
org/springframework/boot/loader/Launcher.class
org/springframework/boot/loader/MainMethodRunner.class
org/springframework/boot/loader/PropertiesLauncher$1.class
org/springframework/boot/loader/PropertiesLauncher$ArchiveEntryFilter.class
org/springframework/boot/loader/PropertiesLauncher$PrefixMatchingArchiveFilter.class
org/springframework/boot/loader/PropertiesLauncher.class
org/springframework/boot/loader/util/
org/springframework/boot/loader/util/SystemPropertyUtils.class
org/springframework/boot/loader/WarLauncher.class