普通javaweb(struts框架)工程转maven工程

由于项目升级,需要将SSH(struts,spring,hibernate)框架改成maven工程。这里本人做一下总结。

1.本人之前的项目结构是这样的,准备用这种方式来转换:右击项目–》configure–》convert to maven project,就不用重构项目转换啦。但是本人实验过后略显麻烦,因为要改很多配置。不如重新创建一个maven工程项目。在这里插入图片描述
2.重新创建一个maven工程项目我是用的 idea工具。创建好后首先将原始项目的jar用依赖的形式导入到pom.xml中。由于jar比较多,就可以用工具类来转换成依赖的形式,如:jdom.1.0.jar 转换成<dependency> <groupId>jdom</groupId> <artifactId>jdom</artifactId> <version>1.0</version> </dependency>
代码如下:

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.jar.*;

/**
 * 通过Jar包SHA1或MD5生成Pom文件
 */
public class JarToPom {

    /**
     * Maven库
     */
//    public static final String nexusUrl = "http://maven.aliyun.com/nexus/service/local/lucene/search";
    public static final String nexusUrl = "http://59.110.144.164/nexus/service/local/lucene/search";

    public static void main(String[] args) {
        File libs = new File("C:\\Users\\admin\\eclipse-workspace\\signalSource-MS-sso1.3\\WebRoot\\WEB-INF\\lib");
        for (File jar : libs.listFiles()) {
            //System.out.println("<!--  " + jar.getName() + " -->");
            if (!getPomByChecksum(jar).isTextOnly()) {
                System.out.println("   ");
                System.out.println(getPomByChecksum(jar).asXML());
            } else if (!getPomByManifest(jar).isTextOnly()) {
                System.out.println("<!--  Search by Manifest -->");
                System.out.println(getPomByManifest(jar).asXML());
            } else {
                System.out.println("<!--  No data was found -->");
            }
            System.out.println();
        }

    }

    /**
     * 通过Jar SHA1返回Pom dependency
     */
    public static Element getPomByChecksum(File file) {
        String checkSum = getCheckSum(file, "SHA1");
        String xml = doGet(nexusUrl + "?sha1=" + checkSum);
        return assemblePomElement(xml);
    }

    /**
     * 通过Jar Manifest返回Pom dependency
     */
    public static Element getPomByManifest(File file) {
        try {
            JarFile jarfile = new JarFile(file);
            Manifest mainmanifest = jarfile.getManifest();
            jarfile.close();
            if (null == mainmanifest) {
                return new DOMElement("dependency");
            }
            String a = null, v = null;
            if (mainmanifest.getMainAttributes().containsKey(new Attributes.Name("Extension-Name"))) {
                a = mainmanifest.getMainAttributes().getValue(new Attributes.Name("Extension-Name"));
            } else if (mainmanifest.getMainAttributes().containsKey(new Attributes.Name("Implementation-Title"))) {
                a = mainmanifest.getMainAttributes().getValue(new Attributes.Name("Implementation-Title"));
            } else if (mainmanifest.getMainAttributes().containsKey(new Attributes.Name("Specification-Title"))) {
                a = mainmanifest.getMainAttributes().getValue(new Attributes.Name("Specification-Title"));
            }
            if (a != null && a.length() != 0) {
                a = a.replace("\"", "").replace(" ", "-");
            }
            if (mainmanifest.getMainAttributes().containsKey(new Attributes.Name("Bundle-Version"))) {
                v = mainmanifest.getMainAttributes().getValue(new Attributes.Name("Bundle-Version"));
            } else if (mainmanifest.getMainAttributes().containsKey(new Attributes.Name("Implementation-Version"))) {
                v = mainmanifest.getMainAttributes().getValue(new Attributes.Name("Implementation-Version"));
            } else if (mainmanifest.getMainAttributes().containsKey(new Attributes.Name("Specification-Version"))) {
                v = mainmanifest.getMainAttributes().getValue(new Attributes.Name("Specification-Version"));
            }
            if (v != null && v.length() != 0) {
                v = v.replace("\"", "").replace(" ", "-");
            }
            String xml = doGet(nexusUrl + "?a=" + a + "&v=" + v);
            return assemblePomElement(xml);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new DOMElement("dependency");
    }

    /**
     * 解析获取的XML 组装dependency
     */
    public static Element assemblePomElement(String xml) {
        Element dependency = new DOMElement("dependency");

        if (xml != null && xml.length() != 0) {
            try {
                Document document = DocumentHelper.parseText(xml);
                Element dataElement = document.getRootElement().element("data");
                if (dataElement.getText() != null && dataElement.getText().length() != 0) {
                    Element artifactElement = dataElement.element("artifact");
                    dependency.add((Element) artifactElement.element("groupId").clone());
                    dependency.add((Element) artifactElement.element("artifactId").clone());
                    dependency.add((Element) artifactElement.element("version").clone());
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
        return dependency;
    }

    /**
     * 发起Get请求
     */
    public static String doGet(String url) {
        String srtResult = null;
        CloseableHttpClient httpCilent = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = httpCilent.execute(httpGet);
            srtResult = EntityUtils.toString(httpResponse.getEntity());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpCilent.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return srtResult;
    }

    /**
     * 计算CheckSum
     */
    public static String getCheckSum(File file, String algorithm) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance(algorithm);
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }

}

`3.依赖转换完成后 分别将java类文件复制到src/main/java目录下,将配置文件放入到src/main/resources目录下,将前端页面复制到src/mian/webapp下。复制完成后记得修改struts.xml中其他包含的struts子文件路径。确保能正确访问到子文件。类似如下:在这里插入图片描述4.项目文件配置完成后点击右侧栏maven–> Lifecycle --> install 。编译通过即转换成功。
以上就是本人转换工程的经验总结,如有不对,还望指正。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值