Maven Learning (GO)

Source: Internet
Author: User

First, preface
Having known that Maven has a reputation for managing Java projects, I've been thinking about learning, so I've consulted a lot of documents. found that the authors of these documents are the big names of Java, most of them are standing in the grasp of a certain MAVEN foundation of the angle of introduction, let me this beginner to see foggy unintelligible. Then went to see Maven's official website, finally has the understanding, but once the hands-on operation actually is mirrors. Alas, there is no way, only to try again and again, after the kind
The ordeal was finally a bit of a prospect. Now write my experience, one to avoid future forgetting, and secondly also give me the same rookie to provide a little bit of convenience. Oh. The topic of this article is practical operation, the principle or trouble everyone to check the Java celebrities articles it. Two articles are recommended here:


Maven 2.0: Compiling, testing, deploying, running


New features of Maven2
[Url]http://www-128.ibm.com/developerworks/cn/opensource/os-maven2/index.html[/url]


Second, MAVEN2 installation
1, first to the official website to download: [Url]http://maven.apache.org/download.html[/url], I chose the latest version of the current maven2.0.4 version
2. Setting environment variable: Maven2 After downloading, I unzip it to my computer's d:\maven204, the directory structure is as follows:
D:\
|--maven204
|--bin
|--conf
|--Core
|--Lib
|--Local


Environment variables: (OS windows2003)
My Computer----Properties----advanced----environment variables, click "System Variables" under the new, input: variable name maven_home; Variable value d:\Maven204, locate the system variable path in the list of system variables, open it, append ";%maven_home%\bin" to the variable value, and set the environment variable to complete.
Check to see if the installation is complete, open a DOS window, enter Mvn–v, and if the following message appears Maven2 has been successfully installed:
X:〉mvn–v
Maven Version 2.0.4


I've already said enough detail. Then I can only say that you are more than my food.



Third, create the first Java project with Maven2
1, select a root directory, my Java working directory is D:\eclipse\workspace
2. Open DOS window with CD command to enter the D:\eclipse\workspace directory
3. Enter the following instructions
D:\ECLIPSE\WORKSPACE>MVN Archetype:create-dgroupid=com.efn-dartifactid=study
After execution of the instruction, the following directory structure will appear:
Study
|--Pom.xml
'--src
|--Main
| '--Java
| '--com
| '--MyCompany
| '--app
| '--App.java
'--Test
'--Java
'--com
'--MyCompany
'--app
'--Apptest.java


Iv. Generating Eclipse Engineering files
The development tool I used was eclipse, so I built the Eclipse project file with Maven's eclipse parameters. Enter the following
1, using DOS CD command to enter the study directory
2. Input directive: MVN Eclipse:eclipse is as follows:
D:\ECLIPSE\WORKSPACE\STUDY>MVN Eclipse:eclipse
After the instruction was executed, two more files were found:. Classpath and. Project


V. Download MAVEN's Eclipse plugin
1. Open Eclipse
2. Open Menu Help-software updates-find and Install
3. Select the second item and go to the next step
4. Click the button "New Remote Site ..."
5, enter in Name: Maven2, enter [Url]http://m2eclipse.codehaus.org/[/url] in the URL
Ok, now everything is automated, all you need to do is to wait and press the button as prompted.


Vi. Introduction of the project
The project file for Eclipse has just been generated and is now imported. Menu File-import, press "Next", select Project file import finished, project management perspective will appear study node, on the project node, press the right mouse button, open the pop-up menu, select Maven2 menu item, pop-up submenu->enable, click on the Open, In the group ID, enter: study.


Opens the study Pom.xml file with the following similar content:


XML code

<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 [Url]http://maven.apache.org/maven-v4_0_0.xsd[/url]" >

<modelVersion>4.0.0</modelVersion>

<groupId>com.efn</groupId>

<artifactId>study</artifactId>

<packaging>jar</packaging>

<version>1.0-SNAPSHOT</version>

<name>maven Quick Start archetype</name>

<url>[url]http://maven.apache.org[/url]</url>

</project>


Note: The role of this file and the role of each node is not the focus of this article, you can go to the official website of maven [Url]http://maven.apache.org/[/url] So far, all the preparations are ready, Now let's take a main main function to access the database as an example to introduce the use in actual development.


Seven, build a rack package
Every Java project uses third-party racks, and the benefit of MAVEN is that they can be managed uniformly and shared across multiple Java projects. All we need to do is edit the Pom.xml file according to the rules. MAVEN is already smart enough to use the MAVEN's Eclipse Plugin Wizard to generate the configuration file as long as the parameters are entered. Our example is to access the Oracle database in JDBC mode. Therefore, the support of JDBC and Oracle's rack package is required. Next we will build these two racks to the study project.


There are two ways to use:
1. Automatic wizard
The first is to use the wizard and let the wizard go directly to Maven's server to download the rack package you need, there is a certain risk, one may MAVEN server does not have what you need, and every time the intelligent maven always go to find that does not exist. Throw a lot of red word hint not to say, not reach the desired goal is to let people depressed. However, to ensure the integrity of the document, the procedure is given. Take JUnit as an example (this thing is no problem, hehe) when the project Maven is enable, pop-up menu maven2, select the "Add Dependency" menu item in the submenu, enter "JUnit" in Query, The wizard automatically lists the related lists for selection. Select the rack package you need most and press the "OK" button.
If the package already exists in your local repository, the wizard only joins the dependency information in Pom.xml, and the contents of the Pom.xml file are as follows

XML code

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://[url]www.w3.org/2001/xmlschema-instance[ /URL] "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 [Url]http://maven. [/URL] "apache.org/maven-v4_0_0.xsd>

<modelVersion>4.0.0</modelVersion>

<groupId>com.efn</groupId>

<artifactId>study</artifactId>

<packaging>jar</packaging>

<version>1.0-SNAPSHOT</version>

<name>maven Quick Start archetype</name>

<url>[url]http://maven.apache.org[/url]</url>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

</dependency>

</dependencies>

</project>

If the local repository is not available, then the wizard will go to Maven's server to download the rack package and download the log in the console output as follows:
06-8-4 09:21 A.M. 07 sec: Local Repository folder "" does not exist
06-8-4 09:21 A.M. 11 sec: Reading/study/pom.xml
06-8-4 09:21 A.M. 11 sec: Local Repository folder "" does not exist
06-8-4 09:21 A.M. 18 sec: Reading/study/pom.xml
06-8-4 09:21 A.M. 18 sec: Local Repository folder "" does not exist
06-8-4 09:21 A.M. 19 sec: Reading/study/pom.xml
06-8-4 09:21 A.M. 19 sec: Local Repository folder "" does not exist
06-8-4 09:36 A.M. 33 sec: Local Repository folder "" does not exist
06-8-4 09:37 A.M. 11 sec: Reading/study/pom.xml
06-8-4 09:37 A.M. 11 sec: Local Repository folder "" does not exist
06-8-4 09:37 A.M. 15 sec: Local Repository folder "" does not exist
06-8-4 09:40 A.M. 07 sec: Local Repository folder "" does not exist
06-8-4 09:40 A.M. 08 sec: Reading/study/pom.xml
06-8-4 09:40 A.M. 08 sec: Local Repository folder "" does not exist
06-8-4 09:46 A.M. 24 sec: Reading/study/pom.xml
06-8-4 09:46 A.M. 24 sec: Local Repository folder "" does not exist
06-8-4 09:46 A.M. 28 sec: Local Repository folder "" does not exist
06-8-4 09:46 A.M. 40 sec: Local Repository folder "" does not exist
06-8-4 09:46 A.M. 47 sec: Local Repository folder "" does not exist
06-8-4 09:46 A.M. 47 sec: Reading/study/pom.xml
06-8-4 09:46 A.M. 47 sec: Local Repository folder "" does not exist
06-8-4 09:46 A.M. 49 sec: Downloading [central], [URL]HTTP://REPO1.MAVEN.ORG/MAVEN2/JUNIT/JUNIT/3.8.1/ Junit-3.8.1.pom[/url]
06-8-4 09:46 A.M. 49 sec: Downloaded [central], [Url]http://repo1.maven.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom [/URL]
06-8-4 09:46 A.M. 51 sec: Downloading [central], [URL]HTTP://REPO1.MAVEN.ORG/MAVEN2/JUNIT/JUNIT/3.8.1/ Junit-3.8.1.jar[/url]
06-8-4 09:47 A.M. 00 SEC: Downloaded [central], [Url]http://repo1.maven.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar [/URL]
----------------------------------------------------------------
Supplemental FIX: If you have an error message and you have a rack at hand, you can use Maven's instructions to localize the installation. For example, when I installed Hibernate's rack package, I told me JTA was unable to download. To require localized installation, the following tips are given:
1) javax.transaction:jta:jar:1.0.1b
Try downloading the file manually from:[url]http://java.sun.com/products/jta.then[/url], install it using the command:
MVN install:install-file-dgroupid=javax.transaction-dartifactid=jta \-dversion=1.0.1b-dpackaging=jar-dfile=/path /to/filepath to dependency:
1) com.efn:mywebapp:war:1.0-snapshot
2) Org.hibernate:hibernate:jar:3.1rc2
3) javax.transaction:jta:jar:1.0.1b
----------
1 required artifact is missing.for Artifact:com.efn: Mywebapp-1.0-snapshot.war This tip is that you can first go to the Sun's website to download the JTA rack package, and then use the command line to install as required, because I already have this package, so I copied to a convenient location, such as C: \, and then at the command line input,
MVN install:install-file-dgroupid=javax.transaction-dartifactid=jta-dversion=1.0.1b-dpackaging=jar-dfile=c:/ Jta.jar
After execution, everything ok!
--------------------------------------------------------------


2. Manual Configuration
Manual configuration is more trouble than automation, but any of the things in their own hands is always eating full of people who also defy not J. and configure it once to benefit from life. It is more important to solve the tasks that automation cannot accomplish. For example, I want to configure the JDBC driver of Oracle Now, use Automation to prompt me to download can not let me change and so on, really tired.
Forget it or do it yourself.
Manual configuration before the first introduction of relevant background knowledge. First look at the concept of the Jar warehouse, MAVEN uses a centralized way to manage the rack package, where the Java project built with MAVEN can reuse the unified management of the rack package, so there must be a uniform storage jar file Place, this is the Jar warehouse, A local warehouse is established for both development and deployment. The location of this warehouse is X:\Documents and settings\administrator.lipeng\.m2\repository (x is the drive letter of your operating system), and you can change the default output location by modifying the configuration file. The configuration file is under the Conf subdirectory under the MAVEN directory. The file is called Settings.xml. Add the following node information in the configuration file to D:/repository. However, it is not recommended to change, as the Eclipse Maven plugin only recognized the default output location. I'm just having a problem with this. It took a while to find out the problem of the output path, maybe I still
No play plug. If anyone solves this problem, don't forget to share it with everyone.
Now let's analyze the structure of the configuration file Pom.xml, in JUnit, for example, in the following format:

XML code

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

</dependency>

</dependencies>

Take a look at the local warehouse directory structure
Repository
'--JUnit
|--JUnit
| '--3.8.1
| '--Junit-3.8.1.jar
Now everyone should understand, needless to say. Divert is. However, note that the first to build the directory after the configuration file, or once saved, the smart plug-in will start to download immediately ...


Now start by manually building the JDBC directory for Oracle and configuring the file, first set up the directory structure as follows:
Repository
'--OJDBC
|--OJDBC
| '--14
| '--Ojdbc-14.jar
If your jar file is named Ojdbc14.jar, write the config file instead of Ojdbc-14.jar:

XML code

<dependency>

<groupId>ojdbc</groupId>

<artifactId>ojdbc</artifactId>

<version>14</version>

</dependency>

So now a complete Pom.xml file is as follows:

XML code

<?xml version= "1.0"?>

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>

<artifactId>myapp</artifactId>

<name>maven Quick Start archetype</name>

<version>1.0-SNAPSHOT</version>

<url>[url]http://maven.apache.org[/url]</url>

<dependencies>

<dependency>

<groupId>ojdbc</groupId>

<artifactId>ojdbc</artifactId>

<version>14</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

</dependency>

</dependencies>

</project>


Save it, then found that the project management perspective has changed a little, in this way, coupled with the JDBC package, you can now start to write programs, build a class and add the main function, write the program as follows:

Java code

public static void Main (string[] args)

{

Connection conn = null;

PreparedStatement PS = null;

ResultSet rs = null;

try {

Class.forName ("Oracle.jdbc.driver.OracleDriver");

conn = Drivermanager.getconnection ("jdbc:oracle:thin:@" (description= (address_list= (address=) (Protocol=TCP 1521) (host=192.168.0.240))) (Connect_data= (SERVER = dedicated) (service_name = db.efriendnet.com))) "," Efnx "," EFNX ");

PS = conn.preparestatement ("SELECT * from Tb_partyinfo");

rs = Ps.executequery ();

while (Rs.next ())

{

System.out.println (rs.getstring ("topic"));

}

} catch (Exception e) {

System.out.print (E.getmessage ());

}

Finally

{

if (rs! = null) {try {rs.close ();} catch (SQLException e) {}}

if (PS! = null) {try {ps.close ();} catch (SQLException e) {}}

IF (conn! = null) {try {conn.close ();} catch (SQLException e) {}}

}

}


Don't forget to import the appropriate package


Viii. Compiling programs
Using MAVEN to build the system, the compilation process is independent. You will not be working with Eclipse's own build tool. So you have to do some work to compile, debug, and run. Previously compiled in DOS command-line mode, now the plug-in is very useful, in Eclipse configuration can be compiled. Very convenient. Let's do an introduction now.


Eclipse has an extension tool that is used to integrate other build tools on the project node right-click, select Properties, in the "Compile" right window select the "New" button, in the dialog box "name" ENTER: study, click "Browse Workspace ..." Lists the list of projects for selection.
After the selection is complete, enter the package in the goals. Don't forget apply. All right, let's run. If everything is fine,
The console will be typing MAVEN's compilation information as follows:
[INFO]---------------------------------------------------------------------
[INFO] Building Maven Quick Start archetype
[INFO] Task-segment: [Package]
[INFO]---------------------------------------------------------------------
[INFO] Resources:resources
[INFO] Using default encoding to copy filtered resources.
[INFO] Compiler:compile
[INFO] Nothing to Compile-all classes is up to date
[INFO] Resources:testresources
[INFO] Using default encoding to copy filtered resources.
[INFO] Compiler:testcompile
[INFO] Nothing to Compile-all classes is up to date
[INFO] Surefire:test
[INFO] Surefire Report Directory:d:\eclipse\workspace\study\target\s
Urefire-reports
-------------------------------------------------------
T E s T s
-------------------------------------------------------
Running Com.efn.AppTest
Tests Run:1, failures:0, errors:0, skipped:0, Time elapsed:0.157 sec
Results:
Tests Run:1, failures:0, errors:0, skipped:0
[INFO] Jar:jar
[INFO] Building Jar:d:\eclipse\workspace\study\target\study-1.0-snapshot.jar
[INFO]----------------------------------------------------------------------------
[INFO] BUILD Successful
[INFO]---------------------------------------------------------------------
[INFO] Total Time:4 Second
[INFO] Finished At:fri 10:55:42 CST 2006
[INFO] Memory 2m/7m
[INFO]--------------------------------------------------------------------
Note, don't forget that every time the program is changed, it will be compiled after this step. Because this is MAVEN's compiler!


Nine, debugging procedures
After the above steps we have completed the software lifecycle under Mave management, but as a program developer we also need to debug. The debugging settings here are the same as for the normal Java program.
First, open the Debug dialog box:
Because it is a generic Java application, so we choose Java application, click the "New" button, enter some corresponding parameters, Apply-debug
OK, everything is fine! I hope you are well!


X. Concluding remarks
This article is simply a guide to MAVEN's operational steps, and there's a lot more to practice. If you find out where there are errors and flaws, you are welcome to criticize!

Maven Learning (GO)

Related Article

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.