1、前言
最近群里小伙伴在問有沒有maven
版本的 jeenotes-ssm「之前是本地 lib 方式」,今天抽空就把改造maven
方式碼出來了,以供參考,這下不用再催我了~
本文環境:MyEclipse + jeenotes-ssm 本地 lib 項目
2、改造過程
首先在MyEclipse/Eclipse
中右擊項目,依次選擇Configure
> Convert to Maven Project
接下來,在彈出來的窗口直接點擊Finish
即可。
點擊Finish
后Eclipse/MyEclipse
會將項目進行重構,重構后項目根目錄會生成默認的pom.xml
文件,具體如下所示:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jeenotes-ssm</groupId>
<artifactId>jeenotes-ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.1 從 maven 倉庫中拉取
顯然這樣是不能用的,此時我們需要把之前用到的lib
轉換為maven
路徑方式,這個地方需要提前說一下,如果你需要引入的依賴比較簡單,也就是項目需要的依賴本地maven
倉庫都有,那么直接在maven
中拉取就好了,但是比較麻煩的是需要手動指定,我舉個例子,比如我項目中使用到了七牛雲7.2.11
版本,那么我就需要手動指定這個依賴,這個過程我需要手動指定 <groupId>
+ <artifactId>
+ <version>
:
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.11</version>
</dependency>
2.2 指定 maven 倉庫地址
如果你覺得這種方式比較費時,那么可以使用maven
加載本地lib
依賴,手動指定maven
倉庫地址,如下提供了工具類GenLibPath.java
,根據項目中的lib路徑
文件自動生成pom
依賴:
public class GenLibPath {
public static void main(String[] args) {
// 項目中存放lib的路徑,也可以指定到外部路徑
String strPath = "/磁盤路徑/jeenotes-ssm2/WebContent/WEB-INF/lib";
File dir = new File(strPath);
String _content = "";
// 根據自己lib存放路徑進行填充lib,其中 ${basedir}代表當前項目路徑
String head = "<properties>\n\t<lib.dir>${basedir}/WebContent/WEB-INF/lib/</lib.dir>\n</properties>\n\n";
File[] files = dir.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
String fileName2 = fileName.replace(".jar", "");
_content += " <dependency>\n";
_content += " <groupId>" + fileName2 + "</groupId>\n";
_content += " <artifactId>" + fileName2 + "</artifactId>\n";
_content += " <version>1.0</version>\n";
_content += " <scope>system</scope>\n";
_content += " <systemPath>${lib.dir}/" + fileName + "</systemPath>\n";
_content += " </dependency>\n\n";
}
System.out.println(head+"<dependencies>\n"+_content+"</dependencies>");
}
}
}
執行后將路徑復制到 pom 文件中,如下所示:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jeenotes-ssm</groupId>
<artifactId>jeenotes-ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<lib.dir>${basedir}/WebContent/WEB-INF/lib/</lib.dir>
</properties>
<dependencies>
<dependency>
<groupId>slf4j-api-1.6.6</groupId>
<artifactId>slf4j-api-1.6.6</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${lib.dir}/slf4j-api-1.6.6.jar</systemPath>
</dependency>
<dependency>
<groupId>protostuff-runtime-1.0.8</groupId>
<artifactId>protostuff-runtime-1.0.8</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${lib.dir}/protostuff-runtime-1.0.8.jar</systemPath>
</dependency>
我省略了一部分...
<dependency>
<groupId>json-20131018</groupId>
<artifactId>json-20131018</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${lib.dir}/json-20131018.jar</systemPath>
</dependency>
<dependency>
<groupId>spring-security-core-3.2.3.RELEASE</groupId>
<artifactId>spring-security-core-3.2.3.RELEASE</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${lib.dir}/spring-security-core-3.2.3.RELEASE.jar</systemPath>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意:${basedir}表示當前項目路徑,最終的目的是讓
<systemPath>
標簽指向的是本地lib
路徑地址,比如你把項目中用到的lib
復制到D盤/lib
目錄里了,因為我不想讓項目因為 lib 變的這么大 ,那么就可以改成如下所示:
<lib.dir>D:\lib\</lib.dir>
好了,到此改造完了,運行一下效果跟之前一樣,除了舊的圖片鏈接掛了。
3、最后補充
本節源碼地址:https://niceyoo.lanzous.com/iczsspi
舊版本 jeenotes-ssm 地址:https://gitee.com/niceyoo/jeenotes-ssm
首發博客地址:https://www.cnblogs.com/niceyoo
18 年專科畢業后,期間一度迷茫,最近我創建了一個公眾號用來記錄自己的成長。