Maven构建应用程序常用配置与NexusRepositoryOSS3安装配置使用

Maven构建应用程序常用配置


使用Maven来构建应用程序,可以非常方便地管理应用相关的资源。众所周知,应用程序中涉及到的一些依赖关系,如Java应用程序依赖jar文件,如果只是手动找到相应的资源,可能需要花费一些时间。而且,即使已经积累了库文件,在未来应用程序升级以后,还要考虑到依赖库文件的升级情况,再次搜索收集。还有一个问题,对应用程序依赖文件的管理是个非常复杂工作,占用存储空间不说,还可能因为应用之间的版本问题导致依赖冲突。使用Maven的pom模型来构建应用程序,可以更加有效地的管理,而且配置内容非常清晰(有时多了,可能pom文件显得有点臃肿)。下面将常用的Maven配置,整理如下,以备参考。首先,整理一个简单的目录,作为快速查询之用:

  1. 设置字符集
  2. 拷贝src/main/resources/资源文件
  3. 编译代码
  4. 、编译打包成jar文件
  5. 构建测试用例配置
  6. 输出依赖jar文件到指定目录
  7. 配置指定的repository
  8. 将应用及其依赖jar文件打成一个jar文件

具体配置的详细内容,如下所示:

1、设置字符集

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

在需要设置字符集的地方,引用${project.build.sourceEncoding}即可。

2、拷贝src/main/resources/资源文件

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <encoding>${project.build.sourceEncoding}</encoding>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <includes>
                                        <include>*.properties</include>
                                        <include>*.xml</include>
                                    </includes>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

3、编译代码

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

可以指定源代码编译级别。

4、编译打包成jar文件

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>without-configs</classifier>
                            <excludes>
                                <exclude>*.properties</exclude>
                                <exclude>*.xml</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </pluginManagement>
</build>

可以指定打包后jar文件的文件名后缀,同时可以设置是否将配置文件也打包到jar文件中。

5、构建测试用例配置

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <skip>true</skip>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

构建应用时,可以配置是否执行测试用例代码,也可以配置如果测试用例未通过是否忽略。

6、输出依赖jar文件到指定目录

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-dependency-plugin</artifactId>
                                    <versionRange>[2.0,)</versionRange>
                                    <goals>
                                        <goal>copy-dependencies</goal>
                                        <goal>unpack</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

上面,和pluginManagement并列的plugins元素中配置的是拷贝依赖jar文件到target/lib目录下面,如果在Eclipse中出现maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e错误,上面pluginManagement元素中的配置,可以解决这个错误提示。

7、配置指定的repository

<repositories>
    <repository>
        <id>cloudera</id>
        <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
</repositories>

如果我们需要要的一些依赖jar文件在maven中央repository中没有,可以在pom文件中配置特定的repository,一般需要配置id和url。

8、将应用及其依赖jar文件打成一个jar文件

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>org.shirdrn.solr.cloud.index.hadoop.SolrCloudIndexer</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>


Nexus Repository OSS 3安装配置使用

Nexus Repository OSS 3是一个开源的仓库管理系统,提供了更加丰富的功能,而且安装、配置、使用起来也更加简单方便。OSS 3版本主要支持的仓库(Repository)包括如下:

  • bower
  • docker
  • maven
  • npm
  • nuget
  • pypi
  • raw
  • rubygems
  • yum

其中,对于上述每种类型的Nexus仓库,都分别具有如下主要3种类型:hosted:本地仓库,可以将我们内部使用的一些Maven项目,发布到该类型仓库,供内部开发人员使用。proxy:代理仓库,用来代理远程公共仓库,比如Maven中央仓库。group:仓库组,用来合并多个类型(hosted/proxy)的仓库。这里,我们主要以支持Java编程的Maven项目依赖管理和构建进行实践,Nexus版本为nexus-3.7.0-04。

安装配置

下载Nexus Repository Manager软件包:

wget https://sonatype-download.global.ssl.fastly.net/nexus/3/nexus-3.7.0-04-unix.tar.gz
tar xvzf nexus-3.7.0-04-unix.tar.gz

解压缩后可以看到,生成nexus-3.7.0-04和sonatype-work两个目录:

[root@ali-bj01-tst-cluster-004 nexus]# ls
nexus-3.7.0-04  sonatype-work

是这两个目录在同一个目录下,例如我这里是在nexus目录下面。可以在etc/nexus-default.properties配置文件中,修改对应的配置项,满足实际需要,这里我直接使用默认的,其它可以使用的配置可以参考官网说明。如果使用自定义的JDK,可以增加如下配置:

export JAVA_HOME=/usr/local/java/jdk1.8.0_144/

启动Nexus Repository Manager,执行如下命令:

bin/nexus start

然后,可以通过Web页面登录浏览,链接如下所示:

http://172.16.117.65:8081

默认用户admin,默认密码为admin123,根据自己的需要修改密码,保证Nexus的安全。Nexus Repository Manager也提供了方便的管理用户和权限的基础功能,可以登录到Web管理页面上进行管理配置,如下图所示:

发布管理

我们需要将自己的Maven项目构建后,发布到Nexus中,供其它项目使用,所以需要进行相关配置,并且具有一定权限才能向Nexus仓库中发布。首先,在settings.xml文件中进行配置,配置内容如下所示:

<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>/Users/yanjun/.m2/repository</localRepository>
    <servers>
         <server>
           <id>maven-releases</id>
           <username>admin</username>
           <password>xxxxxxxxxxxxxxxx</password>
        </server>
         <server>
           <id>maven-snapshots</id>
           <username>admin</username>
           <password>xxxxxxxxxxxxxxxx</password>
        </server>
     </servers>
</settings>

然后,在待要发布到Nexus仓库的Maven项目的pom.xml文件中,增加发布管理配置,如下所示:

<distributionManagement>
    <repository>
        <id>maven-releases</id>
        <name>Nexus ReleaseRepository</name>
        <url>http://172.16.117.65:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>maven-snapshots</id>
        <name>Nexus SnapshotRepository</name>
        <url>http://172.16.117.65:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

需要保证,pom.xml中distributionManagement中的id与setting.xml中配置的server中的id必须相同,才能发布。最后,执行如下命令发布到Nexus的Snapshots仓库:

cd /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer
mvn -s ~/settings.xml deploy

执行上述命令,发布到Nexus的Snapshots仓库的过程,如下所示:

➜  azkaban-app-deployer git:(master) ✗ mvn -s ~/settings.xml deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building azkaban-app-deployer 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ azkaban-app-deployer ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5:compile (default-compile) @ azkaban-app-deployer ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ azkaban-app-deployer ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5:testCompile (default-testCompile) @ azkaban-app-deployer ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ azkaban-app-deployer ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ azkaban-app-deployer ---
[INFO] Building jar: /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/target/azkaban-app-deployer-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-assembly-plugin:2.4.1:single (make-assembly) @ azkaban-app-deployer ---
[INFO] Building jar: /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/target/azkaban-app-deployer-0.0.1-SNAPSHOT-jar-with-dependencies.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ azkaban-app-deployer ---
[INFO] Installing /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/target/azkaban-app-deployer-0.0.1-SNAPSHOT.jar to /Users/yanjun/.m2/repository/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-SNAPSHOT.jar
[INFO] Installing /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/pom.xml to /Users/yanjun/.m2/repository/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-SNAPSHOT.pom
[INFO] Installing /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/target/azkaban-app-deployer-0.0.1-SNAPSHOT-jar-with-dependencies.jar to /Users/yanjun/.m2/repository/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-SNAPSHOT-jar-with-dependencies.jar
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ azkaban-app-deployer ---
Downloading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/maven-metadata.xml
Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1.jar
Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1.jar (36 kB at 3.7 kB/s)
Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1.pom
Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1.pom (3.8 kB at 416 B/s)
Downloading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/maven-metadata.xml
Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/maven-metadata.xml
Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/maven-metadata.xml (780 B at 85 B/s)
Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/maven-metadata.xml
Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/maven-metadata.xml (290 B at 31 B/s)
Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1-jar-with-dependencies.jar
Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1-jar-with-dependencies.jar (3.8 MB at 172 kB/s)
Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/maven-metadata.xml
Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/maven-metadata.xml (1.0 kB at 110 B/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:13 min
[INFO] Finished at: 2018-01-02T15:01:05+08:00
[INFO] Final Memory: 20M/274M
[INFO] ------------------------------------------------------------------------

可以登录到Nexus Repository Manager中,查看已经发布项目jar的Snapshots文件,示例如下图所示:

需要说明的是,我们在Maven项目的pom.xml文件中 ,如果版本号中含有SNAPSHOT,则会自动发布到Snapshots仓库中,例如:

<groupId>cn.xiweiai</groupId>
<artifactId>azkaban-app-deployer</artifactId>
<version>0.0.1-SNAPSHOT</version>

如果将version的值改为不带SNAPSHOT后缀,则会发布到releases仓库中,根据实际需要进行选择。

使用仓库

安装配置好我们私有的Maven仓库,如果我们想使用我们内部发布到Nexus中的Maven依赖,可以直接配置Release Repository:

<repositories>
    <repository>
        <id>maven-releases</id>
        <name>Nexus ReleaseRepository</name>
        <url>http://172.16.117.65:8081/repository/maven-releases/</url>
    </repository>
</repositories>

也可以通过使用group类型的仓库,只需要在Maven项目的pom.xml文件中增加如下配置即可:

<repositories>
    <repository>
        <id>nexus</id>
        <url>http://172.16.117.65:8081/repository/maven-public/</url>
    </repository>
</repositories>

上述配置的该仓库是group类型的仓库组,它会将hosted和proxy类型的仓库合并起来,即:我们在pom.xml中配置依赖时,如果是我们内部的依赖,则会从hosted类型的仓库中去加载;如果是一些开源的依赖,则会通过proxy类型的仓库,代理转发到外部仓库中下载。如果有其它更多非group类型的仓库,也会从这些仓库中查找依赖资源。(原创:时延军(包含链接:http://shiyanjun.cn))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值