2

onedev构建一个实际java spring应用

 2 years ago
source link: https://zhangrr.github.io/posts/20211108-onedev_maven/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Onedev构建一个实际java Spring应用

2021-11-08 2 分钟阅读

上篇简单介绍了 onedev ,这篇我们具体拿个 java spring 的项目来实际编译一下

首先必须确认环境:

onedev 和 agent 都是用 root 安装运行的,然后已经安装了 docker,且 selinux 设置为 disabled,否则会出权限麻烦。

我们用的例子是 spring 的 petclinic,正常的 build 的步骤如下:

git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
./mvnw package

我们首先在 onedev 的 projects 新建一个项目 spring-boot

然后到 clone 下来的源代码目录下

cd spring-petclinic

git init
git add .
git commit -m "Spring boot demo project"

git remote add origin http://192.168.86.101:6610/spring-boot
git push --set-upstream origin master

这样就可以在 spring-boot 里看到代码了

image-20211105113350311

然后看上图,有个紫色灯泡,Enable build support by adding.onedev-buildspec.yml,点那个链接

我们增加一个 build 的步骤,里面再增加两个 job,一个是 Get code ,一个是 build

image-20211108110126299

第一步肯定是把代码拿下来,选择 Checkout Code ,命名为 Get code ,然后其他保持缺省配置,保存

image-20211108110303911

第二步就是 build 代码,选择 Execute Shell/Batch Commands,然后实际是启动了一个 docker 镜像来执行 build 的过程

image-20211108110435271

  • Image 填入:maven:3.5-jdk-8-alphine

  • Commands 填入:

    unset MAVEN_CONFIG
    cat << EOF >> /root/.m2/settings.xml 
    <settings>
      <proxies>
        <proxy>
          <id>default</id>
          <active>true</active>
          <protocol>http</protocol>
          <host>192.168.1.10</host>
          <port>3128</port>
        </proxy>
      </proxies>
    </settings>
    EOF
    ./mvnw package
    

    解释一下,maven:3.5-jdk-8-alphine 这个镜像,如果在里面执行 mvnw package ,会报错 repository 的错,所以必须把 MAVEN_CONFIG 的环境变量给删除,另外整个 build 过程会去拉 N 多包和配置,大概200多兆,如果不翻墙,基本是失败。所以这里强制 mvn 使用了代理!!!否则 build 一天都不会成功。

第三步是配置 mvn repository的缓存,大家不想每次build都去下一遍依赖包吧,在 More Settings 设置

image-20211108111027932

在Caches里填入:

  • key: maven-cache

  • path: /root/.m2/repository

image-20211108111208785

然后执行 Build , 然后等待,第一次时间会很长,终于 Successful 了

image-20211108111629258

接下来的步骤就可以用 Dockerfile 生成镜像,然后推到 Harbor,再下载或者 gitops下载来各种 yaml 文件,拉 kubectl 和配置文件,就可以推送到 kubernetes 了。

补充一下,在别的地方突然看到有 maven 的阿里镜像地址,记录一下:

    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>*</mirrorOf>
    </mirror>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK