- 공식 사이트 – https://www.sonatype.com/nexus-repository-oss
사내에서 생성된 Jar를 보관하며, 중앙 Maven repoistory의 Mirror 역할을 하여, 빌드시 인터넷을 이용하지 않아도 빌드 될 수 있도록 Nexus를 설치해 줍니다(물론 Mirror 이기 때문에 local repository에 필요한 jar가 없는 경우 Central repository에서 받아 옵니다. 이때는 인터넷이 필요합니다). 또한 Nexus는 maven 뿐만 아니라, docker, gitlfs, yum, pypi, 심지어는 raw repository도 지원하여 파일 서버 역할도 할 수 있습니다.
Nexus역시 https://dev.example.io/nexus 로 접근할 것이기 때문에, jenkins, sonar와 같은 과정이 필요 합니다. 우선 docker 로 Nexus를 실행합니다. DB는 필요하지 않습니다. ~/docker/nexus에서 다음 명령을 실행 합니다.
sudo docker run --detach \
--publish 8004:8081 \
--publish 5000:5000 \
--name nexus \
--restart always \
--volume /home/anakin/docker/nexus/data:/nexus-data \
-e "NEXUS_CONTEXT=nexus" \
sonatype/nexus3
또한 Synology에 쉘로 접속하여 해당 디렉토리(Jenkins 설치편 참조)에 user.conf.nexus를 만듭니다.
location /nexus {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_intercept_errors on;
proxy_http_version 1.1;
proxy_pass http://purple.example.io:8004/nexus;
}
LDAP 연동
nexus는 불행하게도 아직 공식적으로 Gitlab 인증을 지원하지 않고 있습니다. 그래서 Directory Server에 직접 연동하여 사용자 인증 하여야 합니다. (비공식 플러그인이 있지만 해보지는 않았습니다)
Nexus에 어드민으로 로그인 하여 Administration 메뉴를 선택하고 Security의 Realms를 선택 합니다. Available에 있는 LDAP Realm 을 선택하고 오른쪽 Active로 보내고 저장합니다. 그러면 Security밑에 LDAP메뉴가 추가 됩니다. 선택 합시다. Create Connection을 누르고 다음 항목을 채워 넣습니다.
Connection
- Name : LDAP
- LDAP Server Address : ldap://192.168.1.34:389 (NAS의 주소와 port)
- Search base : dc=example,dc=io
- Authentication method : Simple Authentication
- Username or DN : uid=root,cn=users,dc=example,dc=io
- Password : Directory Server 설정시 입력했던 패스워드
User and group
- Base DN : cn=users
- Object class : InetOrgPerson
- User ID attribute : uid
- Real name attribute : cn
- Email attribute : mail
Verify user mapping 을 눌러서 사용자 계정이 보이면 성공입니다. 저장하고 사용자로 로그인 해 봅니다.
개발자가 Java Maven 프로젝트에서 Nexus를 이용하려면 우선 https://maven.apache.org/guides/mini/guide-encryption.html 를 참조하여 개발자 PC의 ~/.m2/settings-security.xml을 만들고 다음과 같은 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">
<servers>
<server>
<id>example</id>
<username>(여기에 아이디)</username>
<password>(여기에 패스워드)</password>
</server>
<server>
<id>snapshot</id>
<username>(여기에 아이디)</username>
<password>(여기에 패스워드)</password>
</server>
<server>
<id>release</id>
<username>(여기에 아이디)</username>
<password>(여기에 패스워드)</password>
</server>
</servers>
<mirrors>
<mirror>
<id>example</id>
<mirrorof>*</mirrorof>
<url>https://dev.example.io/nexus/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<activation>
<activebydefault>true</activebydefault>
</activation>
<repositories>
<repository>
<id>nexus-public</id>
<url>https://dev.example.io/nexus/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatepolicy>always</updatepolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>
이제 각 프로젝트의 pom.xml에 다음 내용을 추가하여 해당 프로젝트가 nexus에 배포 할 수 있도록 합니다.
<distributionManagement>
<repository>
<id>release</id>
<url>https://dev.example.io/nexus/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshot</id>
<url>https://dev.example.io/nexus/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
Role 설정
저희는 Nexus에서 파일을 읽기만 하는 nx-readonly 라는 role과 deploy까지 가능한 nx-user role을 운영하고 있습니다. 어드민으로 로그인 하여 상단 기어 버튼을 누르고 좌측 Security-Roles 를 선택 해 줍니다. Create Role버튼을 클릭 하고 nx-readonly Role을 만듭니다.

Deploy도 가능한 nx-user Role도 만들어 줍니다. 이미 있다면 권한만 조정해 줍니다. nx-userschangepw 권한이 있어야 사용자가 자신의 패스워드를 변경할 수 있습니다.

Users 를 선택하고 상단의 Source : 에서 LDAP을 선택한 다음 사용자에게 권한을 부여해 줍니다.


Anonymous 메뉴에서 Allow anonymous users to access the server 항목을 꺼서, 로그인 하지 않으면 사용할 수 없도록 변경 합니다.

Jenkins에서 Nexus 이용
설치된 Nexus를 Jenkins 이용하기위해서 개발자 PC의 ~/.m2 에 settings.xml을 추가 해 주었는데요, Jenkins에서 Maven 프로젝트를 빌드할때는 해당 파일이 없으므로 Central Maven Repository를 이용하게 됩니다. 설치된 local Nexus를 이용하게 하기 위해선 jenkins 컨테이너의 /var/jenkins_home/.m2 디렉토리에 xml 파일을 docker cp 명령으로 넣는 방법이 있습니다.
다른 좀더 유연한 방법으로는 Config File Provider Plugin 를 이용한 방법입니다. Jenkins 플러그인 관리에서 Config File Provider Plugin을 설치 한 후, Jenkins 관리-Managed files를 선택 합니다.

Add a new Config 를 클릭 합니다. Global Maven settings.xml 을 선택 합니다. 적당한 이름과 Content를 채워 넣습니다.

<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">
<servers>
<server>
<id>example</id>
</server>
<server>
<id>snapshot</id>
</server>
<server>
<id>release</id>
</server>
</servers>
<mirrors>
<mirror>
<id>example</id>
<mirrorof>*</mirrorof>
<url>https://dev.example.io/nexus/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<activation>
<activebydefault>true</activebydefault>
</activation>
<repositories>
<repository>
<id>nexus-public</id>
<url>https://dev.example.io/nexus/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatepolicy>always</updatepolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>
Server Credentials 를 눌러서 이용하려는 서버 ID example, snapshot, release 에 대한 계정 정보를 넣어 주어야 합니다. 추가를 클릭하고 Add 에서 Jenkins 를 선택 합니다. Nexus 계정을 추가 해 줍니다.

추가를 눌러서 snapshot 과 release에 대해서도 추가 해 줍니다. 계정정보가 같으므로 이미 입력했던 값을 선택해 줍니다.

이제 이용하려는 프로젝트의 구성-빌드-고급을 선택 합니다. Global Settings file 에서 provided global settings.xml 을 선택하고, Provided Global Settings 에서 방금 생성했던 설정 파일 환경을 선택해 줍니다.
