私服 Nexus 在Java web 项目中的使用

在第一章,我们在Maven 中在settings.xml 中配置了 中央服务器,所以,我们第一步,要把在settings.xml 配置的中央服务器地址改成 我们自己的私服Nexus 对外提供

服务的地址。 私服Nexus 搭建 配置请参考 http://blog.csdn.net/liuc0317/article/details/10345403

window-->Preferences


Maven--> User Settings


点击 open file 

修改 settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository 
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository -->
  <localRepository>D:/maven/.m2/repository</localRepository>

  <pluginGroups>
  
  </pluginGroups>

 
  <proxies>
   
  </proxies>

 
  <servers>
     <server>  
        <id>nexus-snapshots</id>  
        <username>deployment</username>  
        <password>deployment</password>  
    </server>
  </servers>
 
  <mirrors>
    <mirror>  
        <id>nexus</id>  
        <name>internal nexus repository</name>  
        <url>http://11.203.0.82:8081/nexus/content/groups/public/</url>  
        <mirrorOf>central</mirrorOf>  
    </mirror> 
  </mirrors>
  
 
</settings>
说明:

localRepository: 表示 仓库的位置,用来存储从服务器下载到本地jar 包的地址。

servers 是Nexus 提供的 镜像 的ID 和 用户名和密码,

Mirrors 提供 Nexus  public Repositories  的地址 ,是四个 服务 统一对外 暴露的地址。

这样 前期的配置 就完成了,就可以 对 以后创建的 maven 项目进行 文件管理了。


第三章,了解了Nexus 的基本使用和对外提供服务,这一章,我们看在Java web 项目中的使用。

首先,使用Maven 创建一个Java Web 项目. 

在左侧项目列表空白处右键 New->other


New->Maven project


Next 


注:第一个多选框是 创建一个 普通的 java 项目 ,不是java web 项目。

Next 


选中 maven-archetype-webapp

Next


groupId:com.soliucheng.me  
artifactId:maven 

这两项目的设置 是此项目的坐标,使用 此坐标 可以让其他的项目对此项目进行引用。

最后 Finish.

项目建成功后的 最终结构如图:


修改和配置Java web 项目。

第一步,我们认识一个这个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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.soliucheng.me</groupId>
  <artifactId>mavenWeb</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mavenWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>mavenWeb</finalName>
  </build>
</project>
解释: dependencies 这个 是项目依赖的 jar 文件。

             可以 包含 多个 Jar 文件,这些 依赖的 Jar 文件 通过 坐标来获取。

现在 项目中依赖 Junit 3.8.1 ,那么 我们改成 4.8.2 看看 他会不会 从私服 上 给我们下载对应的 Junit 4.8.2 的包呢。

修改完 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.soliucheng.me</groupId>
  <artifactId>mavenWeb</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mavenWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>	
  </dependencies>
  <build>
    <finalName>mavenWeb</finalName>
  </build>
</project>



同时,也把项目中依赖的 jar 包版本 给更新了,在仓库中也多了版本的包。

最后,把我们配置的镜像地址配置到 pom.xml 中。

 <!-- 配置部署的远程仓库 -->  
    <distributionManagement>  
        <snapshotRepository>  
            <id>nexus-snapshots</id>  
            <name>nexus distribution snapshot repository</name>  
            <url>http://11.203.0.82:8081/nexus/content/repositories/snapshots/</url>  
        </snapshotRepository>  
    </distributionManagement>  

id的内容 一定要与 settings.xml 中的 server 中的id  内容相同。

接下来,我们要把maven 的webapp 项目转成 java web 项目,

点击项目右键--> Properties--> Maven --> Project Facets


convert to faceted form...


选中 Dynamic web Module 要把版本 更改成 2.5 的,这样 就可以使用 tomcat 6 来发布项目了。 点OK 

项目结构如图:


此时,并没有在 mavenWeb 子目录下成生 src/main/java  , src/test/java 和 src/test/resources

这些需要 我们手动创建一下,就可以使用了。并布署 到 Tomcat 6 中。



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值