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

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

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

l  创建指向nexus 服务器 public 路径的镜像

    <mirrors>

        <mirror>

            <id>nexus</id>

            <mirrorOf>*</mirrorOf>

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

        </mirror>

    </mirrors>

l  创建 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>

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

    <activeProfiles>

         <activeProfile>nexus</activeProfile>

    </activeProfiles>

l  设置仓库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>



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

l  远程仓库设置:

<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 版本的设置。

l  远程仓库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> 部分的名字保持一致



(3 ) m2eclipse 创建 Maven 工程

l  File->New->Maven->Maven Project

l  选择maven-achetype-quickstart

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

l  点击finish 创建 Maven 工程



(4 ) pom.xml 文件设置:

l  远程仓库设置(前述)

l  远程仓库distribute 设置(前述)

l  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>

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

<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>

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

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

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

</dependency>

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

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

<exclusions>

<exclusion>

<artifactId>hadoop-core</artifactId>

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

</exclusion>

</exclusions>

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

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

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

             <optional>true</optional>

        </dependency>



(5 )部署工程到仓库

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

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

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

n  Goals处输入 clean deploy

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

n  点击Run 执行

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

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

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


3、创建基于 Maven 的 Web 工程

  File->New->Web->Dynamic Web Project创建 Web 工程

  右击Web 工程, Configure->Convert to Maven Project

  指定groupId, , artifactId 和版本号, Packaging 选择 war

  配置pom.xml 文件