前言
创作开始时间:2021年8月21日09:37:06
话不多说,直接给出解决方案。(核心:system scope下添加local jar)
正文
原项目结构:
.
├── lib
│ ├── com.gzoltar-0.1.1-jar-with-dependencies.jar
│ ├── commons-io-2.5.jar
│ ├── dom4j-2.0.0-RC1.jar
│ ├── json-simple-1.1.1.jar
│ ├── log4j-1.2.17.jar
│ ├── org.eclipse.core.contenttype_3.5.0.v20150421-2214.jar
│ ├── org.eclipse.core.jobs_3.7.0.v20150330-2103.jar
│ ├── org.eclipse.core.resources_3.10.1.v20150725-1910.jar
│ ├── org.eclipse.core.runtime_3.11.1.v20150903-1804.jar
│ ├── org.eclipse.equinox.common_3.7.0.v20150402-1709.jar
│ ├── org.eclipse.equinox.preferences_3.5.300.v20150408-1437.jar
│ ├── org.eclipse.jdt.core_3.11.2.v20160128-0629.jar
│ ├── org.eclipse.osgi_3.10.102.v20160118-1700.jar
│ └── org.eclipse.text-3.5.101.jar
├── LICENSE
├── project.xml
├── README.md
├── src
│ ├── cofix
│ ├── fl
│ └── sbfl
可以看到没有maven项目去管理它的依赖,只有一个lib下面存了jar包。
打包也只能用eclipse里面的fatjar插件,不是很方便。
那么怎么转为maven项目呢?尤其是不知道lib里面对应的group、artifact、version id的情况下
下面是解决方案:
解决方案
直接把这些lib里面的jar包当成system scope下的local jar来打包即可!
编写pom.xml如下
<project xmlns="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0)"
xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance")
xsi:schemaLocation="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0) [https://maven.apache.org/xsd/maven-4.0.0.xsd">](https://maven.apache.org/xsd/maven-4.0.0.xsd">)
<modelVersion>4.0.0</modelVersion>
<groupId>apr.confix</groupId>
<artifactId>simfix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Provide maven support for SimFix.</description>
<prerequisites>
<maven>3.3.9</maven>
</prerequisites>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8
</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- parse command line arguments -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
<scope>system</scope>
<systemPath>${basedir}/lib/commons-cli-1.4.jar</systemPath>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>gzoltar</artifactId>
<version>0.1.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/com.gzoltar-0.1.1-jar-with-dependencies.jar
</systemPath>
</dependency>
<dependency>
<groupId>commons</groupId>
<artifactId>io</artifactId>
<version>2.5</version>
<scope>system</scope>
<systemPath>${basedir}/lib/commons-io-2.5.jar</systemPath>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/dom4j-2.0.0-RC1.jar</systemPath>
</dependency>
<dependency>
<groupId>json</groupId>
<artifactId>simple</artifactId>
<version>1.1.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/json-simple-1.1.1.jar</systemPath>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>system</scope>
<systemPath>${basedir}/lib/log4j-1.2.17.jar</systemPath>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>contenttype</artifactId>
<version>3.5.0.v20150421-2214</version>
<scope>system</scope>
<systemPath>${basedir}/lib/org.eclipse.core.contenttype_3.5.0.v20150421-2214.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>jobs</artifactId>
<version>3.7.0.v20150330-2103</version>
<scope>system</scope>
<systemPath>${basedir}/lib/org.eclipse.core.jobs_3.7.0.v20150330-2103.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>resources</artifactId>
<version>3.10.1.v20150725-1910</version>
<scope>system</scope>
<systemPath>${basedir}/lib/org.eclipse.core.resources_3.10.1.v20150725-1910.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>runtime</artifactId>
<version>3.11.1.v20150903-1804</version>
<scope>system</scope>
<systemPath>${basedir}/lib/org.eclipse.core.runtime_3.11.1.v20150903-1804.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.eclipse.equinox</groupId>
<artifactId>common</artifactId>
<version>3.7.0.v20150402-1709</version>
<scope>system</scope>
<systemPath>${basedir}/lib/org.eclipse.equinox.common_3.7.0.v20150402-1709.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.eclipse.equinox</groupId>
<artifactId>org.eclipse.equinox.preferences</artifactId>
<version>3.5.300.v20150408-1437</version>
<scope>system</scope>
<systemPath>${basedir}/lib/org.eclipse.equinox.preferences_3.5.300.v20150408-1437.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>core</artifactId>
<version>1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/org.eclipse.jdt.core_3.11.2.v20160128-0629.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>org.eclipse.osgi</artifactId>
<version>3.10.102.v20160118-1700</version>
<scope>system</scope>
<systemPath>${basedir}/lib/org.eclipse.osgi_3.10.102.v20160118-1700.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>org.eclipse.text</artifactId>
<version>3.5.101</version>
<scope>system</scope>
<systemPath>${basedir}/lib/org.eclipse.text-3.5.101.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Add support for packaging xxx-jar-with-dependencies.jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>cofix.main.Main</mainClass>
</manifest>
</archive>
<!-- doesn't work -->
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
上面的版本号这些都是乱写的,(目测)没关系,只要local jar路径对了就行。
此外,还需要把src文件夹改成src/main/java的形式。
然后以后打包及获取依赖就用:mvn clean package 以及 mvn dependency:copy-dependency 就行。非常方便。
小结
以上。
创作结束时间:2021年8月21日09:45:33
参考文献
- 3 ways to add local jar to maven project https://roufid.com/3-ways-to-add-local-jar-to-maven-project
- How to add local jar files to a Maven project? https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project?rq=1
- How to add local jar files to a Maven project? https://intellipaat.com/community/6786/how-to-add-local-jar-files-to-a-maven-project
- Fat Jar Eclipse Plug-In fjep.sourceforge.net
- Eclipse在线安装FatJar插件 https://blog.csdn.net/weixin_39921821/article/details/81428520 (fatjar在新版本eclipse(2019年以后版本)已经装不了了)
- The system scope dependency jar can’t be packaged into fat jar when mvn clean install #5510 https://github.com/spring-projects/spring-boot/issues/5510
- maven-assembly-plugin doesn’t add dependencies with system scope https://stackoverflow.com/questions/2588502/maven-assembly-plugin-doesnt-add-dependencies-with-system-scope
- How to include system dependencies in war built using maven https://stackoverflow.com/questions/19065666/how-to-include-system-dependencies-in-war-built-using-maven
- Maven 2 assembly with dependencies: jar under scope “system” not included https://stackoverflow.com/questions/2065928/maven-2-assembly-with-dependencies-jar-under-scope-system-not-included