0
点赞
收藏
分享

微信扫一扫

maven引入本地或者第三方jar(scope 为 system 的 jar 包)

心如止水_c736 2022-02-13 阅读 120
mavenjarjava

由于项目中需要引入第三方jar包,但是由于是外部jar,不太适合放入公司内部maven仓库。故采用本地jar包形式引入,或者称之为第三方jar包。方法如下:

1.放入若需jar包,放在工程src目录同级就行。

2.pom.xml引入

        <dependency>
          <groupId>dingding</groupId>
          <artifactId>dingding</artifactId>
          <version>2.8</version>
          <scope>system</scope>
          <systemPath>${project.basedir}/lib/taobao-sdk-java.jar</systemPath>
        </dependency>

3.打包处理

先贴出关键点。

 通过system方式引入的jar,无法正常写入MANIFEST.MF文件,需要加入上述划线内容。

为了方便理解,我贴一下代码对比图

 我再贴一下原始代码

  <profiles>
    <profile>
      <id>dev</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </profile>

    <profile>
      <id>docker</id>
      <build>
        <finalName>${project.artifactId}</finalName>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <targetPath>${project.build.directory}/config</targetPath>
          </resource>
          <resource>
            <directory>src/main/scripts</directory>
            <targetPath>${project.build.directory}/bin</targetPath>
          </resource>
          <resource>
            <directory>src/main/static</directory>
            <targetPath>${project.build.directory}/static</targetPath>
          </resource>
        </resources>
        <plugins>
          <!-- 1、设置jar的入口类 -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <!--<version>2.6</version>-->
            <configuration>
              <archive>
                <addMavenDescriptor>false</addMavenDescriptor>
                <manifest>
                  <addClasspath>true</addClasspath>
                  <classpathPrefix>lib/</classpathPrefix>
                  <useUniqueVersions>false</useUniqueVersions>
                  <mainClass>xx.xx.XXXApplication</mainClass>
                </manifest>
                <manifestEntries>
                  <Class-Path>config/ . lib/neu-cryptography-net-1.0.0.jar lib/sun.misc.BASE64Decoder-1.0.0.jar</Class-Path>
                </manifestEntries>
              </archive>
            </configuration>
          </plugin>

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>
                    ${project.build.directory}/lib
                  </outputDirectory>
                  <stripVersion>false</stripVersion>
                </configuration>
              </execution>
            </executions>
          </plugin>

          <!-- docker的maven插件,官网:https://github.com/spotify/docker-maven-plugin -->
          <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>1.2.2</version>
            <configuration>
              <imageName>${docker.user.name}/XXX-app:${docker.image.tag}</imageName>
              <dockerDirectory>${basedir}/docker</dockerDirectory>
              <pushImage>false</pushImage>
              <labels>
                <label>revision=${image.revision}</label>
              </labels>
              <buildArgs>
                <appVersion>${project.version}</appVersion>
                <serviceId>XXX</serviceId>
                <appConfigPath>${app.config.path}</appConfigPath>
                <appHome>${app.home}</appHome>
              </buildArgs>
              <!-- copy the service's jar file from target into the root directory of the image -->
              <resources>
                <resource>
                  <directory>${basedir}/target</directory>
                  <targetPath>rootfs/${app.home}</targetPath>
                  <include>${project.artifactId}.jar</include>
                </resource>
                <resource>
                  <directory>${basedir}/src/main/scripts</directory>
                  <targetPath>rootfs/${app.home}/scripts</targetPath>
                </resource>
                <resource>
                  <directory>${basedir}/src/main/resources</directory>
                  <targetPath>rootfs/${app.home}/config</targetPath>
                  <excludes>
                    <exclude>src/main/resources/config/dev/*</exclude>
                  </excludes>
                </resource>
                <resource>
                  <directory>${basedir}/target/lib</directory>
                  <targetPath>rootfs/${app.home}/lib</targetPath>
                </resource>
                <resource>
                  <directory>${basedir}/src/main/static</directory>
                  <targetPath>rootfs/${app.home}/static</targetPath>
                </resource>
              </resources>
            </configuration>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>build</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

引入时很方便,本地自测很顺利,没想到到了测试环境maven打包却踩了大坑(本地打包和测试环境打包配置不一样,测试环境是docker 容器里运行的,docker容器里的lib目录里也有对应jar,系统就是找不到)。坑了四五天才搞定,一直报 class not found。当时还搞不定就打算放到公司maven仓库算了。因为网上说的,大多数都是spring boot形式的打包,和MANIFEST.MF相关的没有说到。

我顺便贴一下我网上找的那些对我无效的,都是我踩过的坑。

1.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<version>2.10</version>
	<executions>
		<execution>
			<id>copy-dependencies</id>
			<phase>compile</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<outputDirectory>${project.build.directory}/${project.build.finalName}/lib</outputDirectory>
				<!--这里指定包含 system scope -->
				<includeScope>system</includeScope>
			</configuration>
		</execution>
		<!--打包 scope 为 compile 的 jar 包-->
		<execution>
			<id>copy-dependencies2</id>
			<phase>compile</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<outputDirectory>${project.build.directory}/${project.build.finalName}/lib
				</outputDirectory>
				<includeScope>compile</includeScope>
			</configuration>
		</execution>
	</executions>
</plugin>

2.

 3.

 

我这种方法的作用就是往MANIFEST.MF追加自己加入的scope为system类型的jar包路径,这样系统才能找到对应的jar包位置并引入。

附:JAR包中的MANIFEST.MF文件详解以及编写规范

举报

相关推荐

0 条评论