jar项目改造成maven项目「jeenotes-ssm maven版本」

1、前言

最近群里小伙伴在问有没有maven版本的 jeenotes-ssm之前是本地 lib 方式」,今天抽空就把改造maven方式码出来了,以供参考,这下不用再催我了~

本文环境:MyEclipse + jeenotes-ssm 本地 lib 项目

2、改造过程

首先在MyEclipse/Eclipse中右击项目,依次选择Configure > Convert to Maven Project

接下来,在弹出来的窗口直接点击Finish即可。

点击FinishEclipse/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.lanzoux.com/iczsspi

旧版本 jeenotes-ssm 地址:https://gitee.com/niceyoo/jeenotes-ssm

首发博客地址:https://www.cnblogs.com/niceyoo

18 年专科毕业后,期间一度迷茫,最近我创建了一个公众号用来记录自己的成长。

posted @ 2020-05-25 21:15  niceyoo  阅读(869)  评论(0编辑  收藏  举报