Maven introduces non-existent/custom/third-party jar packages in the central warehouse, and packages them to generate runnable jar packages

If some jar packages do not exist in the maven repository, we have two ways to introduce them into our maven project. Here I have taken the smarthome-nbcodec-1.1.6-SNAPSHOT.jar package as an example.

The first method is relatively simple, just import its jar package into the local mavan warehouse.

mvn install:install-file -Dfile=PATH/smarthome-nbcodec-1.1.6-SNAPSHOT.jar -DgroupId=com.my.smarthome -DartifactId=nbcodec -Dversion=1.1.6-SNAPSHOT -Dpackaging=jar -DgeneratePom=true 

Among them, Dfile is the location of the jar package, -DgroupId, -DartifactId, and -Dversion can be customized according to the situation, and -Dpackaging is the jar. Then it can be imported through pom normally.

	<dependency>
			<groupId>com.my.smarthome</groupId>
			<artifactId>nbcodec</artifactId>
			<version>1.1.6-SNAPSHOT</version>
		</dependency>

The second chapter can put it in the project and then import it

First, create a libs directory in the project to store third-party jar packages.

pom import

	<dependency>
			<groupId>smarthome</groupId>
			<artifactId>nbcodec</artifactId>
			<version>1.1.6-SNAPSHOT</version>
			<scope>system</scope>
			<systemPath>${pom.basedir}/libs/smarthome-nbcodec-1.1.6-SNAPSHOT.jar</systemPath>
		</dependency>

Among them, groupId, artifactId, and version can be customized according to the situation, scope can only be the scope of the system, systemPath is the location of the jar package, and pom.basedir is.

In this way, it can be used directly in the code, but it still cannot run after packaging, so you have to configure the configuration related to packaging.

maven-assembly-plugin plugin packaging

<build>
		<resources>
			<!-- 复制本地扩展jar包到libs目录 -->
			<resource>
				<directory>./libs</directory> <!-- 当前libs目录下的jar包 -->
				<targetPath>${project.build.directory}/dist/${artifactId}-${version}/lib</targetPath> <!-- 复制到本模块打包目录的lib目录 -->
				<includes>
					<include>**/*.jar</include>
				</includes>
			</resource>
			<!--定义了上面的resource, 要想默认的src/main/resources目录生效, 必须显式额外增加下面的配置-->
			<resource>
				<directory>src/main/resources</directory>
				<!-- 表示是否对配置文件中的${}占位符进行解析替换-->
				<filtering>false</filtering>
			</resource>
		</resources>

		<plugins>
			<!-- maven-assembly-plugin插件配置,配置输出目录为dist -->
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<descriptors>
						<descriptor>package.xml</descriptor>
					</descriptors>
					<outputDirectory>${project.build.directory}/dist/</outputDirectory>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.xiangtao.LotApp</mainClass>
							<classpathPrefix>lib/</classpathPrefix>
							<addClasspath>true</addClasspath>
						</manifest>
						<manifestEntries>
							<!-- 在Class-Path下添加配置文件的路径 -->
							<Class-Path>resources/ lib/smarthome-nbcodec-1.1.6-SNAPSHOT.jar</Class-Path>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>

Pay attention to the next position and don’t write it wrong

If it is still unsuccessful in the end, you can decompress the packaged jar package, and then check the generated MANIFEST.MF file to see if all the packages are true.

Guess you like

Origin blog.csdn.net/u012970287/article/details/127613264