标签:软件 取出 myeclipse 建仓 理解 测试文件 容器 第一个 manager
liuliuxundeMacBook-Pro:~ liuxun$ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home liuliuxundeMacBook-Pro:~ liuxun$ java -version java version "1.8.0_102" Java(TM) SE Runtime Environment (build 1.8.0_102-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)2、将maven包解压到用户目录下 如下图所示
<localRepository>/Users/liuxun/maven_repository/repository</localRepository>
package my.test.maven;
public class HelloWorld {
public String sayHello(String name){
return "Hello World :" + name + "!";
}
} 第三步:在/src/test/java/my/test/maven目录下新建测试文件HellTest.javapackage my.test.maven;
import org.junit.Test;
import static junit.framework.Assert.*;
public class HelloTest {
@Test
public void testHello(){
HelloWorld hello = new HelloWorld();
String results = hello.sayHello("maven");
assertEquals("Hello maven!",results);
}
}第四步:在项目MavenHelloWorld 根目录新建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"> <!-- 版本:4.0.0 --> <modelVersion>4.0.0</modelVersion> <!-- 组织名称:暂时使用 组织名称+项目名称 作为组织名称 --> <!-- 组织名称:实际名称 按照访问路径规范设置,通常以功能作为名称:eg: junit spring --> <groupId>my.test.maven</groupId> <!-- 项目名称 --> <artifactId>HelloWorld</artifactId> <!-- 当前项目版本号:同一个项目开发过程中可以发布多个版本,此处标示0.0.1版 --> <!-- 当前项目版本号:每个工程发布后可以发布多个版本,依赖时调取不同的版本,使用不同的版本号 --> <version>0.0.1</version> <!-- 名称:可省略 --> <name>Hello</name> <!-- 依赖关系 --> <dependencies> <!-- 依赖设置 --> <dependency> <!-- 依赖组织名称 --> <groupId>junit</groupId> <!-- 依赖项目名称 --> <artifactId>junit</artifactId> <!-- 依赖版本名称 --> <version>4.9</version> <!-- 依赖范围:test包下依赖该设置 --> <scope>test</scope> </dependency> </dependencies> </project>第五步:
<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>my.test.maven</groupId> <artifactId>HelloFriend</artifactId> <version>0.0.1-SNAPSHOT</version> <name>HelloFriend</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>my.test.maven</groupId> <artifactId>HelloWorld</artifactId> <version>0.0.1</version> <scope>compile</scope> </dependency> </dependencies> </project>第八步:在src/main/java/my/test/maven目录下新建文件HelloFriend.java
package my.test.maven;
import my.test.maven.HelloWorld;
public class HelloFriend {
public String sayHelloToFriend(String name){
HelloWorld hw = new HelloWorld();
String str = hw.sayHello(name)+" I am "+this.getMyName();
System.out.println(str);
return str;
}
public String getMyName(){
return "MAVEN";
}
}第九步:在src/test/java/my/test/maven目录下新建测试文件HelloFriendTest.javapackage my.test.maven;
import static junit.framework.Assert.assertEquals;
import org.junit.Test;
import my.test.maven.Hello;
public class HelloFriendTest {
@Test
public void tesHelloFriend(){
HelloFriend helloFriend = new HelloFriend();
String results = helloFriend.sayHelloToFriend("Jock");
assertEquals("Hello World Jock! I am MAVEN",results);
}
}第十步:在HelloFriend目录下执行命令mvn package 系统报错说没有找到依赖<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies>其中依赖范围scope 用来控制依赖和编译,测试,运行的classpath的关系,主要的是三种依赖关系如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<goals>
<goal>jar-no-fork</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</modules>
继承与聚合的关系
聚合主要为了快速构建项目
继承主要为了消除重复
仓库管理
何为Maven仓库?
package liuxun.maven.One;
public class One {
public String say(String name){
return "Hello "+name+" one";
}
}
OneTest.javapackage liuxun.maven.One;
import org.junit.Test;
import junit.framework.Assert;
public class OneTest {
@Test
public void testSay(){
One one = new One();
String result = one.say("liuxun");
Assert.assertEquals("Hello liuxun one", result);
}
}
One/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> <!-- 群组ID:单位域名反写 --> <!-- <groupId>liuxun.maven</groupId> --> <!-- 项目ID:一个单位只有一个域名,但是一个单位可以做多个项目 也可以理解为组件 --> <artifactId>One</artifactId> <!-- 版本号:用于描述开发过程的阶段性标志 --> <!-- <version>0.0.1-SNAPSHOT</version> --> <!-- 如果打包默认是jar文件 可以设置为ejb ear war jar maven-plugin等 --> <packaging>jar</packaging> <!-- 继承 --> <parent> <groupId>liuxun.maven</groupId> <artifactId>ZParent</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 如果想不进行install父工程 能进行找到的话 必须设置相对路径 --> <relativePath>../ZParent</relativePath> </parent> <!-- 显示的名称 可以省略不写 --> <name>One</name> <!-- 也可以省略不写 --> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- 依赖管理 --> <dependencies> <!-- 具体的依赖 --> <!-- 可以将通用的依赖添加到父工程中 --> <!-- <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> --> <!-- <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> --> <!-- 如果配置了直接依赖的多个版本 1?以最下面 最后一次的配置为准 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> </dependencies> <build> <plugins> <!-- 声明具体的插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <!-- 声明插件的执行位置 --> <executions> <!-- 具体的执行位置 --> <execution> <goals> <goal>jar-no-fork</goal> </goals> <!-- 生命周期中的某个阶段 --> <phase>test</phase> </execution> </executions> </plugin> </plugins> </build> </project>Two项目中
package liuxun.maven.Two;
import liuxun.maven.One.One;
public class Two {
public String say(String name){
One one = new One();
String result = one.say(name);
return result+" two";
}
}
TwoTest.javapackage liuxun.maven.Two;
import org.junit.Test;
import junit.framework.Assert;
public class TwoTest {
@Test
public void testSay(){
Two two = new Two();
String res = two.say("liuxun");
Assert.assertEquals("Hello liuxun one two", res);
}
}
Two/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> <artifactId>Two</artifactId> <packaging>jar</packaging> <!-- 继承 --> <parent> <groupId>liuxun.maven</groupId> <artifactId>ZParent</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 如果想不进行install父工程 能进行找到的话 必须设置相对路径 --> <relativePath>../ZParent</relativePath> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <!-- 直接依赖会覆盖传递过来的依赖 再往下传递时以生效的依赖向下传递--> <!-- 传递到WThree时 log4j的版本为1.2.16 --> <!-- <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> --> <dependency> <groupId>liuxun.maven</groupId> <artifactId>One</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 设置自己的依赖是否向下传递 --> <!-- <optional>true</optional> --> </dependency> </dependencies> </project>项目WThree中
package liuxun.maven.WThree;
import liuxun.maven.Two.Two;
public class Three {
public String say(String name){
Two two = new Two();
String result = two.say(name);
return result+" three";
}
}ThreeTest.javapackage liuxun.maven.WThree;
import org.junit.Test;
import junit.framework.Assert;
public class ThreeTest {
@Test
public void testSay(){
Three three = new Three();
String result = three.say("liuxun");
Assert.assertEquals("Hello liuxun one two three", result);
}
}
WThree/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>liuxun.maven</groupId> <artifactId>WThree</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>WThree</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <dependency> <groupId>liuxun.maven</groupId> <artifactId>Two</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> <!-- 设置排除 --> <!-- 排除依赖必须基于直接依赖中的间接依赖设置可选依赖为false --> <!-- 因为只有直接依赖中设置可选依赖向下传递,才有可能进行排除 --> <!-- 设置当前依赖中是否使用间接依赖 --> <!-- <exclusions> 具体排除 <exclusion> <groupId>liuxun.maven</groupId> <artifactId>One</artifactId> </exclusion> </exclusions> --> </dependency> </dependencies> </project>父项目ZParent中
<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>liuxun.maven</groupId>
<artifactId>ZParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 必须设置为pom 声明此为父工程 专门做配置的 -->
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<abc>1.2.17</abc>
<junit.version>4.9</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${abc}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
<modules>
<module>../One</module>
<module>../Two</module>
<module>../WThree</module>
</modules>
</project>右键父工程中的pom.xml Run As——>Maven build .. 添加参数 clean test 运行结果如下<mirrors>
<mirror>
<!--此处配置所有的构建均从私有仓库中下载 *代表所有,也可以写central -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8080/nexus-2.9.0/content/groups/public/</url>
</mirror>
</mirrors>这个url其实是公共群组的策略<profiles>
<profile>
<id>nexus</id>
<!--所有请求均通过镜像 -->
<repositories>
<repository>
<id>central</id>
<!-- 这个网址根本没有实际作用 因为上面已经配置所有下载的地址 但是不能删除 不然会报节点错误 -->
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<!-- 这个网址根本没有实际作用 因为上面已经配置所有下载的地址 但是不能删除 不然会报节点错误 -->
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>10.3 配置nexus发布版本的权限在setting.xml中找到<servers> 配置如下 <!-- 设置发布时的用户名和密码 -->
<server>
<id> releases </id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id> snapshots </id>
<username>admin</username>
<password>admin123</password>
</server>注意:在发布项目时 配置的发布id要和上面的一致<activeProfiles> <!-- 启用nexus私服策略 --> <activeProfile>nexus</activeProfile> </activeProfiles>然后保存 复制一份将与本地仓库同级目录下的setting.xml替换 保持一致
<distributionManagement> <repository> <id>releases</id> <name>Internal Releases</name> <url>http://localhost:8080/nexus-2.9.0/content/repositories/releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <name>Internal Snapshots</name> <url>http://localhost:8080/nexus-2.9.0/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement>其中两个url分别也是从私服的主页上拷贝的 发布的时候 就会发布到releases和snapshots中
JAVAWEB开发之Maven的入门详解——Maven的安装以及项目的结构和Maven的使用以及私服的搭建与配置
标签:软件 取出 myeclipse 建仓 理解 测试文件 容器 第一个 manager
原文地址:http://blog.csdn.net/u013087513/article/details/71716667