Maven篇----07如何将普通java项⽬转换为maven项⽬本⽂使⽤idea,Eclipse也类似
⼀、引⾔
0. 为何要使⽤maven?
公司项⽬⽬前遇到的问题:
1. 版本管理混乱
版本多的时候,出现问题难以定位,为问题的排查和解决增加了很⼤的难度
2. 项⽬过于复杂, 层次结构不够清晰,显得很臃肿,
不利于新同事理解业务
3. 拉取项⽬代码时,⽂件很⼤下载慢
在下载代码的同时,要拉取⼏百兆的依赖包
4. 容易出现jar包冲突或者是缺少相应的依赖jar包
随着需要的jar包越来越多, 每次引⼊jar包也需要⼩⼼翼翼, 说不准什么时候就会造成jar包冲突,
⽽且也很容易造成某个jar包依赖的jar包没有引⼊⽽导致整个项⽬跑不起来
基于以上, 我们引⼊了maven
⼆、将普通java项⽬转换为maven项⽬
1. 导⼊项⽬并新建l⽂件
⿏标右键: File -> open -> 选择项⽬路径;在项⽬的根⽬录下创建l⽂件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>isc</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>isc_env_dev</name>
<dependencies>
</dependencies>
<build>
<finalName>isc</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. 按照maven项⽬⽬录格式创建⽬录结构
这⾥有以下两个关键⽬录:
a1. Java类⽂件标准⽬录: src/main/java
a2. 资源⽂件(配置)标准⽬录: src/main/resources
a3. js⽂件我的⽬录: src/main/webapp(注意:最终要保证编译打包好的⽂件结构与原始项⽬编译打包后的⽂件结构⼀致)
注意:⽬录需要标记为对应的⽬录结构(选中⽬录右键 Mark Directory as -> Sources Root (或Resources Root))
eclipse导入整个项目3. ⽣成maven项⽬依赖,并导⼊依赖
可以⼿动添加,但是我们的项⽬动辄上百个依赖包。
这⾥推介⽤⼤佬写好的依赖⽣成的⼯具类实现,该⼯具类需要⽤到下⾯的⼀些⼯具类,因此,请先将下
⾯的依赖项复制到l⽂件中。<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
依赖⽣成⼯具类代码如下,请移步,使⽤⽅法是直接将该⼯具类添加到项⽬中,其中“lib”是您的jar⽂件的存放⽬录的路径:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.jsoup.Jsoup;
import des.Document;
import com.alibaba.fastjson.JSONObject;
public class MakePomFromJars {
public static void main(String[] args) throws FileNotFoundException, IOException {
Element dependencys = new DOMElement("dependencies");
File dir = new File("lib");
for (File jar : dir.listFiles()) {
JarInputStream jis = new JarInputStream(new FileInputStream(jar));
Manifest mainmanifest = Manifest();
jis.close();
if (mainmanifest == null) {
continue;
}
String bundleName = MainAttributes().getValue("Bundle-Name");
String bundleVersion = MainAttributes().getValue("Bundle-Version");
Element ele = null;
StringBuffer sb = new Name());
if (bundleName != null) {
bundleName = LowerCase().replace(" ", "-");
sb.append(bundleName + "\t").append(bundleVersion);
ele = getDependices(bundleName, bundleVersion);
// System.out.String());
// System.out.println(ele.asXML());
}
if (ele == null || ele.elements().size() == 0) {
bundleName = "";
bundleVersion = "";
String[] ns = Name().replace(".jar", "").split("-");
for (String s : ns) {
if (Character.isDigit(s.charAt(0))) {
bundleVersion += s + "-";
} else {
bundleName += s + "-";
}
}
if (dsWith("-")) {
bundleVersion = bundleVersion.substring(0, bundleVersion.length() - 1);
}
if (dsWith("-")) {
bundleName = bundleName.substring(0, bundleName.length() - 1);
}
ele = getDependices(bundleName, bundleVersion);
sb.setLength(0);
sb.append(bundleName + "\t").append(bundleVersion);
// System.out.String());
// System.out.println(ele.asXML());
}
ele = getDependices(bundleName, bundleVersion);
if (ele.elements().size() == 0) {
ele.add(new DOMElement("groupId").addText("not find"));
ele.add(new DOMElement("artifactId").addText(bundleName));
ele.add(new DOMElement("version").addText(bundleVersion));
System.out.println(
}
dependencys.add(ele);
// System.out.println();
}
System.out.println(dependencys.asXML());
}
public static Element getDependices(String key, String ver) {
Element dependency = new DOMElement("dependency");
/
/ 设置代理
// System.setProperty("http.proxyHost", "127.0.0.1");
// System.setProperty("http.proxyPort", "8090");
try {
String url = "/solrsearch/select?q=a%3A%22" + key + "%22%20AND%20v%3A%22" + ver
+ "%22&rows=3&wt=json";
Document doc = t(url).ignoreContentType(true).timeout(30000).get();
String elem = doc.body().text();
JSONObject response = JSONObject.parseObject(elem).getJSONObject("response");
if (ainsKey("docs") && JSONArray("docs").size() > 0) {
JSONObject docJson = JSONArray("docs").getJSONObject(0);
Element groupId = new DOMElement("groupId");
Element artifactId = new DOMElement("artifactId");
Element version = new DOMElement("version");
groupId.String("g"));
artifactId.String("a"));
version.String("v"));
dependency.add(groupId);
dependency.add(artifactId);
dependency.add(version);
}
} catch (Exception e) {
e.printStackTrace();
}
return dependency;
}
}
运⾏结果:
使⽤时,只需要将运⾏结果字符串复制到l⽂件中。
注意:
项⽬依赖jar⼀般有三类,⼀类是公共jar,maven官⽹可以查到;第⼆类是公司⾃⼰的jar,第三类是第三⽅jar
a1. 当遇到not find,且是公共jar时,可以⾃⼰到maven官⽹上核实。
a2. 当遇到not find且不是公共jar,或者红⾊不识别的,就是⾃有的⼀些jar。如果公司有搭建私服的需要把这些jar管理起来;否则可作为外部jar在pom中引⼊
4. 对项⽬进⾏编译打包
1. 项⽬上右键 Add Framework Support,选择maven,点击OK。在项⽬右侧显⽰MAVEN窗⼝
2. 接下来执⾏mvn clean install 对项⽬进⾏编译
5. 删除多余的⽬录
打包编译成功之后,就可以把不必要的⽬录⼲掉了! 主要是bin⽬录和lib⽬录
三、可能遇到的问题
1. 编译打包都成功,项⽬运⾏起来当操作涉及到数据库时,代码报:invalid bound statement not found 错误
问题分析:这个问题出现在⽤xml配置mybatis的mapper时,不到xml具体配置就会报这个错
可能原因1:xml⾥⾯的namespace不对或者id和mapper⾥⾯的⽅法名不⼀样,或者parameterType对应
不上,都会出现这种问题。到错误的点修改
可能原因2:mapper写在java⽬录⾥了,编译的时候这个xml⽂件并没有被拉到target⾥⾯,
解决版本:pom⽂件⾥⾯build节点下添加上:
<build>
<finalName>isc</finalName>
<!--解决Intellij构建项⽬时,target/classes⽬录下不存在l⽂件-->
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
<include>**/*.xls</include>
<include>**/*.xlsx</include>
</includes>
</resource>
</resources>
</build>