Java开发环境构建之创建Maven工程

2、创建 Maven 工程

1Maven 的仓库配置之 settings.xml (对当前用户的所有工程有效)

创建指向nexus 私服 public 路径的镜像

    <mirrors>

        <mirror>

            <id>nexus</id>

            <mirrorOf>*</mirrorOf>

            <url>http://10.1.8.26:8081/nexus/content/groups/public</url>

        </mirror>

    </mirrors>

创建 profile ,使用镜像仓库( id要保持一致),使能 snapshots 版本

    <profiles>

        <profile>

             <id>nexus</id>

            <repositories>

                <repository>

                     ......

                    <snapshots>

                         <enabled>true</enabled>

                    </snapshots>

                </repository>

            </repositories>

            <pluginRepositories>

                <pluginRepository>

                     ......

                    <snapshots>

                        <enabled>true</enabled>

                    </snapshots>

                </pluginRepository>

            </pluginRepositories>

        </profile>

</profiles>

激活 profile :注意名字保持一致

    <activeProfiles>

         <activeProfile>nexus</activeProfile>

    </activeProfiles>

设置仓库distribute 的用户名和密码; Maven 工程中 deploy 设置部分的 id 要和这里保持一致,才能使用这里的用户名和密码(见后述)

<servers>

<server>

<id>releases</id>

<username>deployment</username> 

<password>deploy</password> 

</server>

<server>

<id>snapshots</id>

<username>deployment</username> 

<password>deploy</password> 

</server>

</servers>

 

2Maven 的仓库配置之 pom.xml (对当前工程有效,会覆盖 settings.xml 相同设置)

远程仓库设置:

<repositories>

    <repository>

        <id>central</id>

        <name>central</name>

        <url>http://10.1.8.26:8081/nexus/content/groups/public</url>

        <snapshots>

            <enabled>true</enabled>

        <updatePolicy>interval:5</updatePolicy>

        </snapshots>

    </repository>

</repositories>

这里设置了 snapshots 版本的更新策略为每 5分钟检查一次。这个设置会覆盖 settings.xml 中对 snapshots 版本的设置。

远程仓库distribute 设置:

<distributionManagement>

<snapshotRepository>

<id>snapshots</id>

<name>snapshots</name>

<uniqueVersion>true</uniqueVersion>

<url>http://10.1.8.26:8081/nexus/content/repositories/snapshots/</url>

</snapshotRepository>

<repository>

<id>releases</id>

<name>releases</name>

<url>http://10.1.8.26:8081/nexus/content/repositories/releases/</url>

</repository>

</distributionManagement>

这里id 的名字和 settings.xml<servers> 部分的名字保持一致

 

3m2eclipse 创建 Maven 工程

File->New->Maven->Maven Project

选择maven-achetype-quickstart

指定groupId (通常是 com. 公司 . 项目), artifactId (通常是功能模块名,这会作为工程名)和版本号等等

点击finish 创建 Maven 工程

 

4pom.xml 文件设置:

远程仓库设置(前述)

远程仓库distribute 设置(前述)

Maven build选项之设定编译的 JDK 版本和编码:

   <build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.3.2</version>

<configuration>

<source>1.7</source>

<target>1.7</target>

<encoding>${project.build.sourceEncoding}</encoding>

</configuration>

</plugin>

</plugins>

</build>

如果需要同时生成源文件,可以配置:

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-source-plugin</artifactId>

<version>2.1.2</version>

<executions>

<execution>

<phase>package</phase>

<goals>

<goal>jar</goal>

</goals>

</execution>

</executions>

</plugin>

要引入需要的依赖库,添 <dependency> ,如:

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${project.spring.version}</version>

</dependency>

${project.spring.version} <properties> 部分定义,便于 spring 版本的统一升级修改

如果 依赖库版本有冲突,可以使用<exclusions> 排除版本,如:

<exclusions>

<exclusion>

<artifactId>hadoop-core</artifactId>

<groupId>org.apache.hadoop</groupId>

</exclusion>

</exclusions>

或者增加<optional> 来终止依赖传递,如:

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

            <version>${project.spring.version}</version>

             <optional>true</optional>

        </dependency>

 

5 )部署工程到仓库

部署到本地仓库(可以被当前用户的其他工程引用):右击工程名,Run as->Maven install ,可以从 console 窗口看到 JAR 文件被部署到本地仓库的相应位置

部署到Nexus 私服(可以被其他用户的其他工程引用):

右击工程名,Run as->Maven build ...

Goals处输入 clean deploy

如果不想执行测试阶段,勾选Skip T ests

点击Run 执行

注意deploy 时的用户名和密码,就是在 settings 中指定的

至于是deploySNAPSHOT 版本还是 RELEASE 版本,是根据 pom.xml 中的版本是否带 -SNAPSHOT 决定

其实,现在部署构建都是由Hudson 来完成的(后述)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值