一、前提
1.1 创建了4个Maven工程,add,sub工程中分别有一个类实现了加减法,mix则去调用其他三个工程的方法,来实现计算。
add-AddClass
Sub-Substract
mix-Mix
1.2 AddClass:
package add;
public classAddClass {
public int add(int x,int y){
return(x+y);
}
}
Add: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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.a</groupId>
<artifactId>add</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Add</name>
<description>加法</description>
</project>
1.3 Substract:
package sub;
public classSubstract {
public int subStrace(intx,inty){
return (x-y);
}
}
Sub: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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.a</groupId>
<artifactId>sub</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Sub</name>
</project>
1.4 Mix:
package mix;
import sub.Substract;
public classMix {
//Substractsub=new Substract();
//Multiply mix=new Multiply();
public int mix(int x,int y ,int z){
return x-y*z;
}
}
Mix: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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.a</groupId>
<artifactId>mix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Mix</name>
</project>
1.5 nexus私服搭建
将下载的nexus bundle包解压到本地磁盘下面,然后进入路径:
D:\software\nexus-2.8.1-01-bundle\nexus-2.8.1-01\bin\jsw\windows-x86-64执行console-nexus.bat如下图所示,然后就可以在浏览器中通过链接:
nexus访问私服,默认端口为8081,默认用户为admin,密码admin123
1.6 eclipse中设置maven的setting.xml
搭建好nexus私服后,需要在setting.xml设置远程仓库地址
Setting.xml内容如下:
<?xml version="1.0"encoding="UTF-8"?>
<settings>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>central</mirrorOf>
<url>http://127.0.0.1:8081/nexus/content/groups/public</url>;
</mirror>
</mirrors>
<profiles>
<profile>
<id>dev</id>
<repositories>
<repository>
<id>releases</id>
<url>http://127.0.0.1:8081/nexus/content/repositories/releases
</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<url>http://127.0.0.1:8081/nexus/content/repositories/snapshots
</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>thirdparty</id>
<url>http://127.0.0.1:8081/nexus/content/repositories/thirdparty
</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>public</id>
<url>http://127.0.0.1:8081/nexus/content/group/public
</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
<!--<distributionManagement>
<repository>
<id>releases</id>
<name>NexusRelease Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/releases/
</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>NexusSnapshot Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/
</url>
</snapshotRepository>
</distributionManagement>
-->
<servers>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<localRepository>D:\maven\mvnRespo</localRepository>
</settings>
二、实验
2.1 maven install操作
选择sub项目右键run as->maven install,查看控制台,当成功执行后看到结果:
Maven install会将sub项目打包,然后把jar包和pom.xml安装到本地仓库路径下面
2.2 maven deploy
我想执行maven deploy,把项目jar包或者war包上传到私服,让大家共享,所以我选择sub项目右键run as -> runconfigurations->maven build 右键new ,创建如下图所示sub,填写图中标红的地方。
![
![Uploading psb[1]_459247.png . . .]].png](http://upload-images.jianshu.io/upload_images/1237796-3e705c79e7d43fa9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后点击run,查看控制台,发现报错,查找后发现我pom.xml中没有上传到私服的配置
修改sub 的pom.xml如下,其中distributionManagement即为添加的配置,然后在重新run就发现已经deploy到私服了,并且会重新install到本地仓库:
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.a</groupId>
<artifactId>sub</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Sub</name>
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
2.3在mix中访问工程sub方法
首先修改工程mix的类Mix如下,发现无法识别Substract,
只需要很简单一步就可以在mix工程中引用sub工程中方法了
修改mix的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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.a</groupId>
<artifactId>mix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Mix</name>
<dependencies>
<dependency>
<groupId>com.a</groupId>
<artifactId>sub</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>