Nexus私服环境下Spring Initializr配置本地服务

本文档介绍了如何将Spring Initializr项目克隆并配置为使用Nexus私服,包括修改pom.xml文件,配置Maven的servers、mirrors、profiles和activeProfiles,安装Spring Boot CLI,以及在Eclipse中导入和运行项目,最终实现通过本地Nexus服务器提供Spring Initializr服务。

1、Git克隆Spring Initializr

网址:https://github.com/spring-io/initializr

git bash中切换0.6.0分支: git checkout  4b9fddd

或者git bash中切换0.7.0分支: git checkout  6a06f54

2、修改根目录pom.xml文件

在initializr工程根目录的pom.xml文件project标签下添加如下内容,用于将本工程上传到NEXUS 的RELEASES仓库

Nexus 2 版本

<distributionManagement>
   <repository>
      <id>nexus-releases</id>
      <url>http://localhost:8081/nexus/content/repositories/releases/</url>
   </repository>
   <snapshotRepository>
      <id>nexus-snapshots</id>
      <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
   </snapshotRepository>
</distributionManagement>

Nexus 3 版本

<distributionManagement>
    <repository>
        <id>nexus-releases</id>
        <url>http://localhost:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <url>http://localhost:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

3、Maven配置之servers

.m2/setting.xml文件做如下配置,用户可以上传构件到nexus私服

<servers>
    <server>
        <id>nexus-releases</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <server>
        <id>nexus-snapshots</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
</servers>

4、mvn deploy

上述配置执行完之后,即可在控制台窗口中执行mvn deploy命令,将此工程上传到NEXUS私服完毕。

注意:在initializr工程的根目录执行mvn命令。

5、MAVEN设置之mirrors & profiles & activeProfiles

.m2/setting.xml文件做如下配置,使得maven镜像到NEXUS的中央仓库和RELEASES仓库,注意mirrors里面的子元素顺序决定了路径的匹配顺序。

Nexus 2 版本

  <mirrors>
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*, !nexus-releases, !nexus-snapshots</mirrorOf>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
    </mirror>
    <mirror>
      <!--This sends everything else to /releases, -->
      <id>nexus-releases</id>
      <mirrorOf>nexus-releases</mirrorOf>
      <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </mirror>
    <mirror>
      <!--This sends everything else to /snapshots, -->
      <id>nexus-snapshots</id>
      <mirrorOf>nexus-snapshots</mirrorOf>
      <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://repo1.maven.org/maven2/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
          <id>nexus-releases</id>
          <url>http://localhost:8081/nexus/content/repositories/releases/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </repository>
        <repository>
          <id>nexus-snapshots</id>
          <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
          <releases><enabled>false</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://repo1.maven.org/maven2/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
        <pluginRepository>
          <id>nexus-releases</id>
          <url>http://localhost:8081/nexus/content/repositories/releases/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </pluginRepository>
        <pluginRepository>
          <id>nexus-snapshots</id>
          <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
          <releases><enabled>false</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
     </pluginRepositories>
    </profile>
  </profiles>
  
  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>  

Nexus 3 版本

<mirrors>
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/repository/maven-public/</url>
    </mirror>
</mirrors>
<profiles>
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://repo1.maven.org/maven2/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
          <id>nexus-releases</id>
          <url>http://localhost:8081/repository/maven-releases/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </repository>
        <repository>
          <id>nexus-snapshots</id>
          <url>http://localhost:8081/repository/maven-snapshots/</url>
          <releases><enabled>false</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://repo1.maven.org/maven2/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
        <pluginRepository>
          <id>nexus-releases</id>
          <url>http://localhost:8081/repository/maven-releases/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>false</enabled></snapshots>
        </pluginRepository>
        <pluginRepository>
          <id>nexus-snapshots</id>
          <url>http://localhost:8081/repository/maven-snapshots/</url>
          <releases><enabled>false</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
     </pluginRepositories>
    </profile>
</profiles>
  
<activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
</activeProfiles>  

6、安装Spring Boot CLI

0.6.0:网址 http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.0.3.RELEASE/

0.7.0:网址 http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.2.RELEASE/

从以上网址下载zip压缩文件包到本地,解压缩后,参考install.txt文件,将spring-boot-cli/2.0.3.RELEASE\bin的完整路径添加到环境变量PATH中,这样可以在命令行中输入spring命令符,为了校验是否安装成功,输入spring --version查看版本。

7、导入Eclipse工程

将Spring Initializr导入到Eclipse工程中,Java 要求1.8版本以上,并且使用Maven。

8、创建Groovy文件

在项目根目录创建一个新目录(例如,spring-initializr),进入目录,创建initializr.groovy文件,内容如下:

@Grab('io.spring.initializr:initializr-web:0.6.0.RELEASE')
@Grab('spring-boot-starter-web')
class InitializrService { }

9、编译Groovy文件

cd进入spring-initializr,Terminal中执行 

spring run initializr.groovy

10、网页生成结果

在浏览器中打开http://localhost:8080,便可使用Spring Initializr了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值