设置本地Nexus存储库并从Maven部署WAR文件

Maven Central充当中央存储库管理器,其中二进制工件由不同的团队/公司/个人上载并与世界其他地方共享。 就像github和其他对源代码控制非常有效的源代码存储库一样,这些存储库管理器还充当您自己生成的二进制工件的部署目标。

设置本地存储库管理器具有多个优点。 最主要的是,它们充当Maven Central之间的高度可配置代理,因此每个人都不必从中央存储库下载所有依赖项。 另一个主要原因是要控制团队内部临时生成的工件。 使用存储库管理器的原因详细说明了完整的好处。

本技术提示将展示如何设置本地Nexus信息库管理器,以及将工件(快照和发行版)推送到其中。

让我们开始吧!

安装和配置本地Nexus存储库

  1. 下载并解压缩最新的Nexus OSS 默认管理员的登录名/密码是admin / admin123。 默认的部署登录名/密码是Deployment / deployment123。
  2. 以以下方式启动Nexus:
    nexus-2.11.2-03> ./bin/nexus start
    Starting Nexus OSS...
    Started Nexus OSS.
    

    日志可以看成是:

    nexus-2.11.2-03> tail -f logs/wrapper.log

    或者,您可以从控制台本身中显示日志的位置开始:

    nexus-2.11.2-03> ./bin/nexus console
    Running Nexus OSS...
    wrapper  | --> Wrapper Started as Console
    wrapper  | Launching a JVM...
    jvm 1    | Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=192m; support was removed in 8.0
    jvm 1    | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
    jvm 1    |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
    jvm 1    | 
    
    . . .
    
    jvm 1    | 2015-02-24 00:17:28,706-0800 INFO  [jetty-main-1] *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer - Running
    jvm 1    | 2015-02-24 00:17:28,706-0800 INFO  [WrapperListener_start_runner] *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer - Started
    jvm 1    | 2015-02-24 00:17:30,713-0800 INFO  [ar-4-thread-3] *SYSTEM org.sonatype.nexus.proxy.maven.routing.internal.ManagerImpl - Updated and published prefix file of "Public Repositories" [id=public]
    
  3. 将Maven设置文件(〜.m2 / settings.xml)配置为包括以下默认部署用户名和密码:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
       <servers>
          <server>
            <id>deployment</id>
            <username>deployment</username>
            <password>deployment123</password>
         </server>
      </servers>
    </settings>
    

将快照部署到本地Nexus存储库

  1. github.com/javaee-samples/javaee7-simple-sample签出一个简单的Java EE示例。
  2. 通过以下方式创建WAR文件并将其部署到本地Nexus存储库中:
    javaee7-simple-sample> mvn deploy
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building javaee7-simple-sample 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ javaee7-simple-sample ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ javaee7-simple-sample ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 4 source files to /Users/arungupta/workspaces/javaee7-simple-sample/target/classes
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ javaee7-simple-sample ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory /Users/arungupta/workspaces/javaee7-simple-sample/src/test/resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ javaee7-simple-sample ---
    [INFO] No sources to compile
    [INFO] 
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ javaee7-simple-sample ---
    [INFO] No tests to run.
    [INFO] 
    [INFO] --- maven-war-plugin:2.3:war (default-war) @ javaee7-simple-sample ---
    [INFO] Packaging webapp
    [INFO] Assembling webapp [javaee7-simple-sample] in [/Users/arungupta/workspaces/javaee7-simple-sample/target/javaee7-simple-sample]
    [INFO] Processing war project
    [INFO] Copying webapp resources [/Users/arungupta/workspaces/javaee7-simple-sample/src/main/webapp]
    [INFO] Webapp assembled in [33 msecs]
    [INFO] Building war: /Users/arungupta/workspaces/javaee7-simple-sample/target/javaee7-simple-sample.war
    [INFO] 
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ javaee7-simple-sample ---
    [INFO] Installing /Users/arungupta/workspaces/javaee7-simple-sample/target/javaee7-simple-sample.war to /Users/arungupta/.m2/repository/org/javaee7/sample/javaee7-simple-sample/1.0-SNAPSHOT/javaee7-simple-sample-1.0-SNAPSHOT.war
    [INFO] Installing /Users/arungupta/workspaces/javaee7-simple-sample/pom.xml to /Users/arungupta/.m2/repository/org/javaee7/sample/javaee7-simple-sample/1.0-SNAPSHOT/javaee7-simple-sample-1.0-SNAPSHOT.pom
    [INFO] 
    [INFO] >>> wildfly-maven-plugin:1.0.2.Final:deploy (default) > package @ javaee7-simple-sample >>>
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ javaee7-simple-sample ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ javaee7-simple-sample ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ javaee7-simple-sample ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory /Users/arungupta/workspaces/javaee7-simple-sample/src/test/resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ javaee7-simple-sample ---
    [INFO] No sources to compile
    [INFO] 
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ javaee7-simple-sample ---
    [INFO] No tests to run.
    [INFO] Skipping execution of surefire because it has already been run for this configuration
    [INFO] 
    [INFO] --- maven-war-plugin:2.3:war (default-war) @ javaee7-simple-sample ---
    [INFO] Packaging webapp
    [INFO] Assembling webapp [javaee7-simple-sample] in [/Users/arungupta/workspaces/javaee7-simple-sample/target/javaee7-simple-sample]
    [INFO] Processing war project
    [INFO] Copying webapp resources [/Users/arungupta/workspaces/javaee7-simple-sample/src/main/webapp]
    [INFO] Webapp assembled in [3 msecs]
    [INFO] Building war: /Users/arungupta/workspaces/javaee7-simple-sample/target/javaee7-simple-sample.war
    [INFO] 
    [INFO] <<< wildfly-maven-plugin:1.0.2.Final:deploy (default) < package @ javaee7-simple-sample <<<
    [INFO] 
    [INFO] --- wildfly-maven-plugin:1.0.2.Final:deploy (default) @ javaee7-simple-sample ---
    Feb 24, 2015 1:02:46 AM org.xnio.Xnio <clinit>
    INFO: XNIO version 3.2.2.Final
    Feb 24, 2015 1:02:46 AM org.xnio.nio.NioXnio <clinit>
    INFO: XNIO NIO Implementation Version 3.2.2.Final
    Feb 24, 2015 1:02:46 AM org.jboss.remoting3.EndpointImpl <clinit>
    INFO: JBoss Remoting version 4.0.3.Final
    [INFO] 
    [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ javaee7-simple-sample ---
    Downloading: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/1.0-SNAPSHOT/maven-metadata.xml
    Downloaded: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/1.0-SNAPSHOT/maven-metadata.xml (783 B at 4.0 KB/sec)
    Uploading: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/1.0-SNAPSHOT/javaee7-simple-sample-1.0-20150224.090247-2.war
    Uploaded: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/1.0-SNAPSHOT/javaee7-simple-sample-1.0-20150224.090247-2.war (6 KB at 90.4 KB/sec)
    Uploading: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/1.0-SNAPSHOT/javaee7-simple-sample-1.0-20150224.090247-2.pom
    Uploaded: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/1.0-SNAPSHOT/javaee7-simple-sample-1.0-20150224.090247-2.pom (3 KB at 53.2 KB/sec)
    Downloading: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/maven-metadata.xml
    Downloaded: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/maven-metadata.xml (297 B at 13.8 KB/sec)
    Uploading: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/1.0-SNAPSHOT/maven-metadata.xml
    Uploaded: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/1.0-SNAPSHOT/maven-metadata.xml (783 B at 20.7 KB/sec)
    Uploading: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/maven-metadata.xml
    Uploaded: http://localhost:8081/nexus/content/repositories/snapshots/org/javaee7/sample/javaee7-simple-sample/maven-metadata.xml (297 B at 6.7 KB/sec)
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 4.398 s
    [INFO] Finished at: 2015-02-24T01:02:47-08:00
    [INFO] Final Memory: 22M/310M
    [INFO] ------------------------------------------------------------------------
    

    快照存储库经过几次构建后,可以在localhost:8081 / nexus /#view-repositories; snapshots〜browsestorage中看到,如下所示:

    techtip73-nexus快照存储库-1024x749

    实际的存储库存储在../sonatype-work/nexus目录中。 这是在解压缩Nexus OSS捆绑包的同时创建的。

将发行版部署到本地Nexus存储库

  1. 清除任何以前执行的发行版:
    javaee7-simple-sample> mvn release:clean -P release
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building javaee7-simple-sample 1.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-release-plugin:2.5.1:clean (default-cli) @ javaee7-simple-sample ---
    [INFO] Cleaning up after release...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 0.912 s
    [INFO] Finished at: 2015-02-24T02:18:53-08:00
    [INFO] Final Memory: 10M/245M
    [INFO] ------------------------------------------------------------------------
    
  2. 准备下一个版本:
    javaee7-simple-sample> mvn release:prepare -P release
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building javaee7-simple-sample 1.3-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-release-plugin:2.5.1:prepare (default-cli) @ javaee7-simple-sample ---
    [INFO] Verifying that there are no local modifications...
    [INFO]   ignoring changes on: **/pom.xml.releaseBackup, **/pom.xml.next, **/pom.xml.tag, **/pom.xml.branch, **/release.properties, **/pom.xml.backup
    [INFO] Executing: /bin/sh -c cd /Users/arungupta/workspaces/javaee7-simple-sample && git rev-parse --show-toplevel
    [INFO] Working directory: /Users/arungupta/workspaces/javaee7-simple-sample
    [INFO] Executing: /bin/sh -c cd /Users/arungupta/workspaces/javaee7-simple-sample && git status --porcelain .
    [INFO] Working directory: /Users/arungupta/workspaces/javaee7-simple-sample
    [WARNING] Ignoring unrecognized line: ?? release.properties
    [INFO] Checking dependencies and plugins for snapshots ...
    What is the release version for "javaee7-simple-sample"? (org.javaee7.sample:javaee7-simple-sample) 1.3: : 
    What is SCM release tag or label for "javaee7-simple-sample"? (org.javaee7.sample:javaee7-simple-sample) v1.3: : 
    What is the new development version for "javaee7-simple-sample"? (org.javaee7.sample:javaee7-simple-sample) 1.4-SNAPSHOT: : 
    [INFO] Transforming 'javaee7-simple-sample'...
    [INFO] Not generating release POMs
    [INFO] Executing goals 'clean verify'...
    [WARNING] Maven will be executed in interactive mode, but no input stream has been configured for this MavenInvoker instance.
    [INFO] [INFO] Scanning for projects...
    [INFO] [INFO]                                                                         
    [INFO] [INFO] ------------------------------------------------------------------------
    [INFO] [INFO] Building javaee7-simple-sample 1.3
    [INFO] [INFO] ------------------------------------------------------------------------
    
    . . .
    
    [INFO] Executing: /bin/sh -c cd /Users/arungupta/workspaces/javaee7-simple-sample && git symbolic-ref HEAD
    [INFO] Working directory: /Users/arungupta/workspaces/javaee7-simple-sample
    [INFO] Executing: /bin/sh -c cd /Users/arungupta/workspaces/javaee7-simple-sample && git push https://github.com/javaee-samples/javaee7-simple-sample.git refs/heads/master:refs/heads/master
    [INFO] Working directory: /Users/arungupta/workspaces/javaee7-simple-sample
    [INFO] Release preparation complete.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 01:04 min
    [INFO] Finished at: 2015-02-24T02:55:59-08:00
    [INFO] Final Memory: 11M/245M
    [INFO] ------------------------------------------------------------------------
    
  3. 执行发布:
    javaee7-simple-sample> mvn javadoc:jar source:jar release:perform -P release
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building javaee7-simple-sample 1.4-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-javadoc-plugin:2.10.1:jar (default-cli) @ javaee7-simple-sample ---
    
    . . .
    
    [INFO] [INFO] ------------------------------------------------------------------------
    [INFO] [INFO] BUILD FAILURE
    [INFO] [INFO] ------------------------------------------------------------------------
    [INFO] [INFO] Total time: 5.820 s
    [INFO] [INFO] Finished at: 2015-02-24T02:58:16-08:00
    [INFO] [INFO] Final Memory: 31M/312M
    [INFO] [INFO] ------------------------------------------------------------------------
    [INFO] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project javaee7-simple-sample: Failed to deploy artifacts: Could not transfer artifact org.javaee7.sample:javaee7-simple-sample:war:1.3 from/to deployment (http://localhost:8081/nexus/content/repositories/releases/): Failed to transfer file: http://localhost:8081/nexus/content/repositories/releases/org/javaee7/sample/javaee7-simple-sample/1.3/javaee7-simple-sample-1.3.war. Return code is: 400, ReasonPhrase: Bad Request. -> [Help 1]
    

    注意,该命令以错误结尾。 这类似于此处报告的内容,但奇怪的是文件仍在Nexus上载。 这是来自localhost:8081 / nexus /#view-repositories; releases〜browsestorage的快照,同时尝试测试多个版本并想知道这些“虚假”错误消息:

    techtip73-nexus-发布库

    该错误将需要更多的调试,但现在至少可以将快照和发行版本存储在本地Nexus存储库中。

更新: Manfred Moser通过发送拉取请求帮助调试了该错误。 现在,此错误已消失,而应显示类似以下内容:

[INFO] Uploaded: http://localhost:8081/nexus/content/repositories/releases/org/javaee7/sample/javaee7-simple-sample/1.8/javaee7-simple-sample-1.8-sources.jar (3 KB at 74.7 KB/sec)
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] BUILD SUCCESS
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] Total time: 5.673 s
[INFO] [INFO] Finished at: 2015-02-26T03:50:18+05:30
[INFO] [INFO] Final Memory: 31M/320M
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] Cleaning up after release...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:10 min
[INFO] Finished at: 2015-02-26T03:50:30+05:30
[INFO] Final Memory: 13M/245M
[INFO] ------------------------------------------------------------------------

您学习了如何设置本地Nexus Repository并向其推送快照和发布版本。 随后的博客将展示如何将此存储库用于CI / CD。

请享用!

翻译自: https://www.javacodegeeks.com/2015/03/setup-local-nexus-repository-and-deploying-war-file-from-maven.html