Convert a common Java project into a MAVEN project

Source: Internet
Author: User
Tags maven central

Turn your project into a MAVEN project

Right-click the project->add Framework Supprot->maven->ok
Automatically transforms the project into a MAVEN project structure and automatically generates the Pom.xml file.

We know that the SRC directory is our source file directory, and the project source files are compiled into the classes directory after the IDE (either idea or eclipse), but when idea compiles the source file, By default, any. xml files under the SRC directory are not placed in the classes directory, and when we use the MyBatis Persistence layer framework, the mapping files are accustomed to being placed under the corresponding package in the SRC directory.
Then the MyBatis configuration is usually as follows:

<mapper resource="com/ssm/chapter13/sqlMapper/RoleMapper.xml"/>

At run time, an exception that cannot find a RoleMapper.xml file occurs.
Solution Of course we can put the mapping file and its directory in the MAVEN project resources directory, and then the MyBatis configuration is as follows:

<mapper resource="sqlMapper/RoleMapper.xml"/>

We can also not change the original habit, do not move the mapping file, modify the Pomm.xml file, as follows:

<build>  <resources>      <resource>        <directory>src/main/java</directory>        <includes>          <include>**/*.xml</include>        </includes>        <filtering>true</filtering>      </resource>    </resources></build>

When idea compiles our source code, the map file is also copied to the classes directory.

A build-dependent tool class

Although idea generates a Pom.xml file for us, the jar files that our project relies on are not generated in the Pom.xml file, and if our project relies on a very large number of jar packages, then we have a lot of work to find in the MAVEN central repository. Here is a gadget class that can generate well-formed MAVEN dependencies based on the jar package file name.

Package Top.sqmax;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 Com.alibaba.fastjson.JSONObject; Public classMakepomfromjars { Public Static void Main(string[] args)throwsFileNotFoundException, IOException {Element Dependencys =New DomElement("Dependencys"); File dir =NewFile ("F:\\Books\\SSM\\Project-lib "); for(File Jar:dir.Listfiles()) {Jarinputstream JIS =NewJarinputstream (NewFileInputStream (jar)); Manifest mainmanifest = JIS.getmanifest(); Jis.Close();if(Mainmanifest = =NULL) {System.Err.println(Jar.GetName());Continue; } String bundlename = Mainmanifest.getmainattributes().GetValue("Bundle-name"); String bundleversion = mainmanifest.getmainattributes().GetValue("Bundle-version"); Element ele =NULL; System. out.println(Jar.GetName()); StringBuffer SB =NewStringBuffer (jar.GetName());if(Bundlename! =NULL) {bundlename = Bundlename.toLowerCase().Replace(" ","-"); Sb.Append(bundlename+"\ t").Append(bundleversion); Ele =getdependices(Bundlename, bundleversion); System. out.println(sb.)toString()); System. out.println(Ele.Asxml()); }if(Ele = =NULL|| Ele.Elements().size() ==0) {Bundlename =""; Bundleversion =""; string[] ns = jar.GetName().Replace(". Jar","").Split("-"); for(String S:ns) {if(Character.IsDigit(S.charAt(0)) {Bundleversion + = s +"-"; }Else{Bundlename + = s +"-"; }                }if(Bundleversion.EndsWith("-") {bundleversion = bundleversion.substring(0, Bundleversion.length() -1); }if(Bundlename.EndsWith("-") {bundlename = Bundlename.substring(0, Bundlename.length() -1); } ele =getdependices(Bundlename, bundleversion); Sb.SetLength(0); Sb.Append(bundlename+"\ t").Append(bundleversion); System. out.println(sb.)toString()); 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)); } dependencys.Add(Ele); System. out.println(); } System. out.println(Dependencys.Asxml()); } Public StaticElementgetdependices(string key, String ver) {Element dependency =New DomElement("Dependency");//Set up proxy        //System.setproperty ("Http.proxyhost", "127.0.0.1");        //System.setproperty ("Http.proxyport", "8090");        Try{String URL ="Http://search.maven.org/solrsearch/select?q=a%3A%22"+ key +"%22%20and%20v%3a%22"+ ver +"%22&rows=3&wt=json"; Org.Jsoup.nodes.DocumentDoc = Jsoup.Connect(URL).Ignorecontenttype(true).Timeout(30000).Get(); String Elem = doc.Body().text(); Jsonobject response = Jsonobject.parseobject(Elem).Getjsonobject("Response");if(Response.ContainsKey("Docs") && Response.Getjsonarray("Docs").size() >0) {Jsonobject Docjson = response.Getjsonarray("Docs").Getjsonobject(0); Element groupId =New DomElement("GroupId"); Element Artifactid =New DomElement("Artifactid"); Element Version =New DomElement("Version"); GroupId.AddText(Docjson.getString("G")); Artifactid.AddText(Docjson.getString("a")); Version.AddText(Docjson.getString("V")); Dependency.Add(GROUPID); Dependency.Add(Artifactid); Dependency.Add(version); }        }Catch(Exception e) {e.Printstacktrace(); }returnDependency }}

Note that this tool class also relies on 3 jar packages, as follows:

<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>

Finally, let's test our tool class with the F:\书籍\ssm\project-lib dozens of jar packages that the project relies on in the current directory:

After running our tool class, we will print these dependencies on the console, just copy them to the Pom.xml file, and then format it with idea.

Of course, this tool class will be some small bugs, may be based on some jar package file name generated by the dependency error, but we can manually modify those errors generated by the dependency.

With the above steps, we can forward a generic Java project as a MAVEN project.

Tool class in the original text see: https://my.oschina.net/zhhzhfya/blog/735050

Convert a common Java project into a MAVEN project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.