这是本节的多页打印视图。 点击此处打印.

返回本页常规视图.

Java

和Java相关的内容

1 - 安装JDK

安装JDK

使用sdkman安装

使用 sdkman 管理JDK:

$ sdk list java
$ sdk install java 11.0.15-zulu

Downloading: java 11.0.15-zulu

In progress...

###################################################################################### 100.0%

Repackaging Java 11.0.15-zulu...

Done repackaging...

Installing: java 11.0.15-zulu
Done installing!


Setting java 11.0.15-zulu as default.

apt-get 自动安装

直接 apt-get 安装最新版本的oracle jdk8:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

备注:ubuntu 20.04 下已经不支持,报错" 此 PPA 不支持 focal"。只能改用手工安装方式。

中间选择接收 oracle 的协议,然后继续安装。(吐糟一下: oracle 服务器的下载速度真慢,即使是我放在国外的VPS,号称千兆网络,也只有300-500k的速度……)

如果安装有多个版本的 JDK,可以用下面的命令来设置默认的JDK:

sudo apt-get install oracle-java8-set-default

安装完成之后运行 java –version 查看安装结果:

# java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

此时java命令是通过将 /usr/lib/jvm/java-8-oracle/jre/bin/java 文件一路链接到 /etc/alternatives/java 和 /usr/bin/java 的:

root@sky2:~# which java
/usr/bin/java
root@sky2:~# ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 Oct  5 08:40 /usr/bin/java -> /etc/alternatives/java
root@sky2:~# ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 39 Oct  5 08:40 /etc/alternatives/java -> /usr/lib/jvm/java-8-oracle/jre/bin/java

并且没有设置 JAVA_HOME。推荐设置好 JAVA_HOME 并将 JAVA_HOME/bin 目录加入PATH,修改 .bashrc 文件:

# java
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/
export PATH=$JAVA_HOME/bin:$PATH

再执行 source ~/.bashrc 载入即可。

手工安装JDK

如果手工安装,可以从oracle网站下载安装文件,如 jdk-8u281-linux-x64.tar.gz(对于ubuntu不要下载rpm版本)。

手工解压缩到安装路径:

gunzip jdk-8u281-linux-x64.tar.gz
tar xvf jdk-8u281-linux-x64.tar
# ubuntu20.04下这个这个目录已经存在
# sudo mkdir /usr/lib/jvm
sudo mv jdk1.8.0_281 /usr/lib/jvm/jdk8

JAVA_HOME 和 PATH 设置类似:

# java
export JAVA_HOME=/usr/lib/jvm/jdk8/
export PATH=$JAVA_HOME/bin:$PATH

参考资料

2 - 安装Maven

安装并配置Maven

安装maven

用sdkman安装

$ sdk install maven

Downloading: maven 3.8.5

In progress...

Setting maven 3.8.5 as default.

配置代理服务器

export MAVEN_OPTS="-DsocksProxyHost=192.168.0.30 -DsocksProxyPort=10808"

3 - 安装Artifacory

安装并配置Artifacory

安装

准备安装:

wget -qO - https://api.bintray.com/orgs/jfrog/keys/gpg/public.key | sudo apt-key add -
echo "deb https://jfrog.bintray.com/artifactory-debs focal main" | sudo tee /etc/apt/sources.list.d/jfrog.list
sudo apt update

执行安装:

sudo apt install jfrog-artifactory-oss

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  jfrog-artifactory-oss
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 484 MB of archives.
After this operation, 989 MB of additional disk space will be used.
Get:1 https://jfrog.bintray.com/artifactory-debs focal/main amd64 jfrog-artifactory-oss amd64 7.37.15 [484 MB]
Fetched 484 MB in 38s (12.9 MB/s)                                              
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LC_TIME = "zh_CN.UTF-8",
	LC_MONETARY = "zh_CN.UTF-8",
	LC_ADDRESS = "zh_CN.UTF-8",
	LC_TELEPHONE = "zh_CN.UTF-8",
	LC_NAME = "zh_CN.UTF-8",
	LC_MEASUREMENT = "zh_CN.UTF-8",
	LC_IDENTIFICATION = "zh_CN.UTF-8",
	LC_NUMERIC = "zh_CN.UTF-8",
	LC_PAPER = "zh_CN.UTF-8",
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
locale: Cannot set LC_ALL to default locale: No such file or directory
Selecting previously unselected package jfrog-artifactory-oss.
(Reading database ... 105976 files and directories currently installed.)
Preparing to unpack .../jfrog-artifactory-oss_7.37.15_amd64.deb ...
dpkg-query: no packages found matching artifactory
Checking if group artifactory exists...
Group artifactory doesn't exist. Creating ...
Checking if user artifactory exists...
User artifactory doesn't exist. Creating ...
Checking if artifactory data directory exists
Removing tomcat work directory
Unpacking jfrog-artifactory-oss (7.37.15) ...
Setting up jfrog-artifactory-oss (7.37.15) ...
Adding the artifactory service to auto-start... DONE

************ SUCCESS ****************
The Installation of Artifactory has completed successfully.

NOTE: It is highly recommended to use Artifactory with an external database (MyS
QL, Oracle, Microsoft SQL Server, PostgreSQL, MariaDB).
      For details about how to configure the database, refer to https://service.
jfrog.org/installer/Configuring+the+Database

Start Artifactory with:
> systemctl start artifactory.service

Check Artifactory status with:
> systemctl status artifactory.service


Installation directory was set to /opt/jfrog/artifactory
You can find more information in the log directory /opt/jfrog/artifactory/var/lo
g
System configuration templates can be found under /opt/jfrog/artifactory/var/etc
Copy any configuration you want to modify from the template to /opt/jfrog/artifa
ctory/var/etc/system.yaml

Triggering migration script, this will migrate if needed ...
Processing triggers for systemd (245.4-4ubuntu3.17) ...

start 并 enable artifactory:

sudo systemctl start artifactory.service
sudo systemctl enable artifactory.service

检验一下:

$ systemctl status artifactory.service
● artifactory.service - Artifactory service
     Loaded: loaded (/lib/systemd/system/artifactory.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-05-06 03:06:34 UTC; 55s ago
   Main PID: 44611 (java)
      Tasks: 0 (limit: 154104)
     Memory: 20.8M
     CGroup: /system.slice/artifactory.service
44611 /opt/jfrog/artifactory/app/third-party/java/bin/java -Djava.util.logging.config.file=/opt/jfrog/artifactory/app/>

May 06 03:06:33 skyserver su[45384]: (to artifactory) root on none
May 06 03:06:33 skyserver su[45384]: pam_unix(su:session): session opened for user artifactory by (uid=0)
May 06 03:06:33 skyserver su[45384]: pam_unix(su:session): session closed for user artifactory
May 06 03:06:33 skyserver su[45497]: (to artifactory) root on none
May 06 03:06:33 skyserver su[45497]: pam_unix(su:session): session opened for user artifactory by (uid=0)
May 06 03:06:34 skyserver su[45497]: pam_unix(su:session): session closed for user artifactory
May 06 03:06:34 skyserver su[45628]: (to artifactory) root on none
May 06 03:06:34 skyserver su[45628]: pam_unix(su:session): session opened for user artifactory by (uid=0)
May 06 03:06:34 skyserver su[45628]: pam_unix(su:session): session closed for user artifactory
May 06 03:06:34 skyserver systemd[1]: Started Artifactory service.

$ ps -ef | grep artifactory                               
sky        39708   33533  0 03:05 pts/0    00:00:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox artifactory

$ nc  -zv  127.0.0.1 8081     
Connection to 127.0.0.1 8081 port [tcp/tproxy] succeeded!

配置Artifactory

访问:

http://192.168.0.40:8081

会被重定向到:

http://192.168.0.40:8082/ui/login/

默认账号为: Username: admin Password: password

设置 baseURL: http://sz.springmesh.io/

(注意在路由器中开启 8081 、 8082 两个端口的端口映射,以便远程访问)

创建仓库时选择 maven ,Repositories created during the Onboarding:

  • libs-snapshot-local
  • maven-remote
  • libs-snapshot

登录之后会要求修改默认的 admin 密码。

配置密码加密方式

为了避免在 maven 的 settings.xml 文件中出现密码的明文,要启用密码加密方式,在 “security” -> “settings” 中修改 “Password Encryption Policy” 为 “support”。

备注: unsupport 是明文,support 是可以明文也可以加密,required 是必须加密。

遇到的特殊问题:我在 settings.xml 中使用明文密码 + artifactory 设置 unsupport 可以工作,在 settings.xml 中使用加密密码 + artifactory 设置 support 也可以工作,但我在 settings.xml 中使用明文密码 + artifactory 设置 required 就会报错 401,不清楚为什么。

配置 maven

修改 maven 的 settings 配置文件 (~/.m2/settings.xml),内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.2.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <servers>
    <server>
      <username>admin</username>
      <password>{lnSEkwEnmhaaaaaaaaaaaaaaaaaaaaaa=}</password>
      <id>central</id>
    </server>
    <server>
      <username>admin</username>
      <password>{lnSEkwEnmaaaaaaaaaaaaaaaaaaaaa=}</password>
      <id>snapshots</id>
    </server>
  </servers>
  <mirrors>
    <mirror>
      <mirrorOf>*</mirrorOf>
      <name>libs-release</name>
      <url>http://192.168.0.40:8081/artifactory/libs-release</url>
      <id>central</id>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <repositories>
        <repository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>libs-release</name>
          <url>http://192.168.0.40:8081/artifactory/libs-release</url>
        </repository>
        <repository>
          <snapshots />
          <id>snapshots</id>
          <name>libs-snapshot</name>
          <url>http://192.168.0.40:8081/artifactory/libs-snapshot</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>libs-release</name>
          <url>http://192.168.0.40:8081/artifactory/libs-release</url>
        </pluginRepository>
        <pluginRepository>
          <snapshots />
          <id>snapshots</id>
          <name>libs-snapshot</name>
          <url>http://192.168.0.40:8081/artifactory/libs-snapshot</url>
        </pluginRepository>
      </pluginRepositories>
      <id>artifactory</id>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>artifactory</activeProfile>
  </activeProfiles>
</settings>

注意:在使用mirror的情况下,mirror的id要和 server 的 id 保持一致,否则会报错 401。

其中密码加密的方式参考官方文档:

https://maven.apache.org/guides/mini/guide-encryption.html

mvn --encrypt-master-password
{jSMOWnoPFgsHVpMvz5VrIt5kRbzGpI8u+9EF1iFQyJQ=}

输入一个master密码,可以理解为是一个加密因子,总之和服务器端admin的实际密码没任何关系,随便输入一堆数字和字母,然后就会得到一组类似 {jSMOWnoPFgsHVpMvz5VrIt5kRbzGpI8u+9EF1iFQyJQ=} 的字符串。

新建 ${user.home}/.m2/settings-security.xml 文件,内容为:

<settingsSecurity>
  <master>{jSMOWnoPFgsHVpMvz5VrIt5kRbzGpI8u+9EF1iFQyJQ=}</master>
</settingsSecurity>

再执行下面的命令加密admin的实际密码:

mvn --encrypt-password
{COQLCE6DU6GtcS5P=}

将得到的 类似 {COQLCE6DU6GtcS5P=} 的加密后的字符串保存在 settings.xml 文件中。

  <servers>
    <server>
      <username>admin</username>
      <password>{lnSEkwEnmhaaaaaaaaaaaaaaaaaaaaaa=}</password>
      <id>central</id>
    </server>
    <server>
      <username>admin</username>
      <password>{lnSEkwEnmaaaaaaaaaaaaaaaaaaaaa=}</password>
      <id>snapshots</id>
    </server>
  </servers>

参考资料

4 - 安装Quarkus

安装并配置Quarkus

安装

参考: https://quarkus.io/get-started/

sdk install quarkus

备注:下载速度有点问题,建议开启代理