mirror of
https://github.com/s4u/maven-settings-action.git
synced 2026-02-12 00:05:49 +08:00
114
README.md
114
README.md
@ -25,13 +25,14 @@ or use automatic tools like [Dependabot](https://dependabot.com/).
|
||||
# Usage
|
||||
See [action.yml](action.yml)
|
||||
|
||||
Create default ```settings.xml```:
|
||||
## default ```settings.xml```
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
```
|
||||
|
||||
Create ```settings.xml``` with servers section:
|
||||
## ```settings.xml``` with servers section
|
||||
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
@ -39,7 +40,42 @@ steps:
|
||||
servers: '[{"id": "serverId", "username": "username", "password": "password"}]'
|
||||
```
|
||||
|
||||
Create ```settings.xml``` with mirrors section:
|
||||
## ```settings.xml``` with servers section and additional configuration
|
||||
|
||||
``` yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
with:
|
||||
servers: |
|
||||
[{
|
||||
"id": "serverId",
|
||||
"configuration": {
|
||||
"item1": "value1",
|
||||
"item2": {
|
||||
"item21": "value21",
|
||||
"item22": "value22"
|
||||
}
|
||||
}
|
||||
}]
|
||||
```
|
||||
|
||||
result will be:
|
||||
|
||||
```xml
|
||||
<server>
|
||||
<id>serverId</id>
|
||||
<configuration>
|
||||
<item1>value1</item1>
|
||||
<item2>
|
||||
<item21>value21</item21>
|
||||
<item22>value22</item22>
|
||||
</item1>
|
||||
</configuration>
|
||||
</server></servers>
|
||||
```
|
||||
|
||||
|
||||
## ```settings.xml``` with mirrors section
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
@ -47,7 +83,7 @@ steps:
|
||||
mirrors: '[{"id": "mirrorId", "name": "mirrorName", "mirrorOf": "mirrorOf", "url": "mirrorUrl"}]'
|
||||
```
|
||||
|
||||
Create ```settings.xml``` with maven properties:
|
||||
## ```settings.xml``` with properties
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
@ -55,23 +91,8 @@ steps:
|
||||
properties: '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]'
|
||||
```
|
||||
|
||||
It is also possible pass in Github Secrets e.g.
|
||||
## ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list
|
||||
|
||||
``` yml
|
||||
with:
|
||||
servers: |
|
||||
[{
|
||||
"id": "sonatype-nexus-snapshots",
|
||||
"username": "${{ secrets.SONATYPE_USERNAME }}",
|
||||
"password": "${{ secrets.SONATYPE_PASSWORD }}"
|
||||
}]
|
||||
|
||||
```
|
||||
|
||||
**Note**: secrets are *not* passed in if the workflow is triggered from a forked repository. See [here](https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets#using-encrypted-secrets-in-a-workflow) for further information. This can be avoided by using `if` triggers on the job e.g. `if: github.event_name == 'push'`.
|
||||
|
||||
|
||||
Create ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
@ -79,7 +100,8 @@ steps:
|
||||
sonatypeSnapshots: true
|
||||
```
|
||||
|
||||
Create ```settings.xml``` with https://repository.apache.org/snapshots/ in repository list
|
||||
## ```settings.xml``` with https://repository.apache.org/snapshots/ in repository list
|
||||
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
@ -87,7 +109,7 @@ steps:
|
||||
apacheSnapshots: true
|
||||
```
|
||||
|
||||
Do not override existing ```settings.xml```, from version 2.0 file is override by default :
|
||||
## Do not override existing ```settings.xml```, from version **2.0** file is override by default :
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
@ -95,7 +117,7 @@ steps:
|
||||
override: false
|
||||
```
|
||||
|
||||
Do not add github to server in ```settings.xml```, by default is added:
|
||||
## Do not add github to server in ```settings.xml```, by default is added:
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
@ -103,7 +125,8 @@ steps:
|
||||
githubServer: false
|
||||
```
|
||||
|
||||
Create ```settings.xml``` with special server item configuration for oracle repository [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9015)
|
||||
## ```settings.xml``` with special server item configuration for oracle repository [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9015)
|
||||
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
@ -111,7 +134,7 @@ steps:
|
||||
oracleServers: '[{"id": "serverId", "username": "username", "password": "password"}]'
|
||||
```
|
||||
|
||||
Create ```settings.xml``` with [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9017)
|
||||
## ```settings.xml``` with [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9017)
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
@ -119,7 +142,46 @@ steps:
|
||||
oracleRepo: true
|
||||
```
|
||||
|
||||
**Note**: When using maven-settings-action in combination with Cache action (actions/cache) it is required to place the cache action **before** maven-settings-action.
|
||||
## GitHub actions secrets
|
||||
|
||||
It is also possible pass in Github Secrets e.g.
|
||||
|
||||
``` yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
with:
|
||||
servers: |
|
||||
[{
|
||||
"id": "sonatype-nexus-snapshots",
|
||||
"username": "${{ secrets.SONATYPE_USERNAME }}",
|
||||
"password": "${{ secrets.SONATYPE_PASSWORD }}"
|
||||
}]
|
||||
```
|
||||
|
||||
**Note**: secrets are *not* passed in if the workflow is triggered from a forked repository. See [here](https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets#using-encrypted-secrets-in-a-workflow) for further information. This can be avoided by using `if` triggers on the job e.g. `if: github.event_name == 'push'`.
|
||||
|
||||
# Notes
|
||||
|
||||
**maven-settings-action** should be put at the latest position before maven run in order to avoid override ```setting.xml``` by another action
|
||||
|
||||
```yml
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.m2/repository
|
||||
key: maven-${{ hashFiles('**/pom.xml') }}
|
||||
restore-keys: maven-
|
||||
|
||||
- uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 8
|
||||
|
||||
- uses: s4u/maven-settings-action@v2.2.0
|
||||
|
||||
- run: mvn verify
|
||||
```
|
||||
|
||||
# License
|
||||
|
||||
|
||||
@ -63,7 +63,7 @@ afterAll(() => {
|
||||
|
||||
test('run with all feature', () => {
|
||||
|
||||
process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword"}]';
|
||||
process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword", "configuration": {"props1": "value1"}}]';
|
||||
process.env['INPUT_ORACLESERVERS'] = '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]';
|
||||
process.env['INPUT_GITHUBSERVER'] = true;
|
||||
|
||||
@ -80,7 +80,7 @@ test('run with all feature', () => {
|
||||
expect(settingsStatus.isFile()).toBeTruthy();
|
||||
expect(settingsStatus.size).toBeGreaterThan(0);
|
||||
|
||||
const settingsBody = fs.readFileSync(settingsPath).toString();
|
||||
const settingsBody = fs.readFileSync(settingsPath).toString().replace(/^ $/mg, '');
|
||||
expect(settingsBody).toBe(`<settings>
|
||||
<interactiveMode>false</interactiveMode>
|
||||
<profiles>
|
||||
@ -186,6 +186,7 @@ test('run with all feature', () => {
|
||||
<id>serverId</id>
|
||||
<username>sUsername</username>
|
||||
<password>sPassword</password>
|
||||
<configuration><props1>value1</props1></configuration>
|
||||
</server>
|
||||
<server>
|
||||
<id>oServerId</id>
|
||||
@ -213,6 +214,7 @@ test('run with all feature', () => {
|
||||
<id>github</id>
|
||||
<username>\${env.GITHUB_ACTOR}</username>
|
||||
<password>\${env.GITHUB_TOKEN}</password>
|
||||
|
||||
</server></servers>
|
||||
<mirrors>
|
||||
<mirror>
|
||||
|
||||
47
settings.js
47
settings.js
@ -50,17 +50,51 @@ function writeSettings(settingsPath, templateXml) {
|
||||
fs.writeFileSync(settingsPath, settingStr);
|
||||
}
|
||||
|
||||
function fillServer(templateXml, templateName, id, username, password) {
|
||||
function jsonToXml(templateXml, xmlTag, json) {
|
||||
for (const key in json) {
|
||||
const keyXml = templateXml.createElement(key);
|
||||
const value = json[key];
|
||||
if ( value instanceof Object) {
|
||||
jsonToXml(templateXml, keyXml, value);
|
||||
} else {
|
||||
keyXml.textContent = value;
|
||||
}
|
||||
xmlTag.appendChild(keyXml);
|
||||
}
|
||||
}
|
||||
|
||||
if (!id || !username || !password) {
|
||||
core.setFailed(templateName + ' must contain id, username and password');
|
||||
function fillServer(templateXml, templateName, id, username, password, configurations) {
|
||||
|
||||
if (!id || ((!username || !password) && !configurations) ) {
|
||||
core.setFailed(templateName + ' must contain id, (username and password) or configuration');
|
||||
return;
|
||||
}
|
||||
|
||||
const serverXml = getTemplate(templateName + '.xml')
|
||||
serverXml.getElementsByTagName('id')[0].textContent = id;
|
||||
serverXml.getElementsByTagName('username')[0].textContent = username;
|
||||
serverXml.getElementsByTagName('password')[0].textContent = password;
|
||||
|
||||
const usernameTag = serverXml.getElementsByTagName('username')[0];
|
||||
if (username) {
|
||||
usernameTag.textContent = username;
|
||||
} else {
|
||||
serverXml.documentElement.removeChild(usernameTag);
|
||||
}
|
||||
|
||||
const passwordTag = serverXml.getElementsByTagName('password')[0];
|
||||
if (password) {
|
||||
passwordTag.textContent = password;
|
||||
} else {
|
||||
serverXml.documentElement.removeChild(passwordTag);
|
||||
}
|
||||
|
||||
const configurationTag = serverXml.getElementsByTagName('configuration')[0];
|
||||
if (configurations) {
|
||||
jsonToXml(templateXml, configurationTag, configurations);
|
||||
} else {
|
||||
if (configurationTag.childNodes.length == 0) {
|
||||
serverXml.documentElement.removeChild(configurationTag);
|
||||
}
|
||||
}
|
||||
|
||||
const serversXml = templateXml.getElementsByTagName('servers')[0];
|
||||
serversXml.appendChild(serverXml);
|
||||
@ -74,7 +108,8 @@ function fillServers(template, templateName) {
|
||||
return;
|
||||
}
|
||||
|
||||
JSON.parse(servers).forEach((server) => fillServer(template, templateName, server.id, server.username, server.password));
|
||||
JSON.parse(servers).forEach((server) =>
|
||||
fillServer(template, templateName, server.id, server.username, server.password, server.configuration));
|
||||
}
|
||||
|
||||
function fillMirror(template, id, name, mirrorOf, url) {
|
||||
|
||||
@ -136,17 +136,94 @@ test('fillServers one server', () => {
|
||||
|
||||
settings.fillServers(xml, 'servers');
|
||||
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml);
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');
|
||||
|
||||
expect(xmlStr).toBe(`<servers>
|
||||
<server>
|
||||
<id>id1</id>
|
||||
<username>username1</username>
|
||||
<password>password1</password>
|
||||
|
||||
</server></servers>`);
|
||||
|
||||
});
|
||||
|
||||
test('fillServers with username and configuration', () => {
|
||||
|
||||
const xml = new DOMParser().parseFromString("<servers/>");
|
||||
|
||||
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';
|
||||
|
||||
settings.fillServers(xml, 'servers');
|
||||
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
|
||||
|
||||
expect(xmlStr).toBe(`<servers>
|
||||
<server>
|
||||
<id>id1</id>
|
||||
<username>username</username>
|
||||
|
||||
<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
|
||||
</server></servers>`);
|
||||
});
|
||||
|
||||
test('fillServers with username, password and configuration', () => {
|
||||
|
||||
const xml = new DOMParser().parseFromString("<servers/>");
|
||||
|
||||
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "password": "password", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';
|
||||
|
||||
settings.fillServers(xml, 'servers');
|
||||
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
|
||||
|
||||
expect(xmlStr).toBe(`<servers>
|
||||
<server>
|
||||
<id>id1</id>
|
||||
<username>username</username>
|
||||
<password>password</password>
|
||||
<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
|
||||
</server></servers>`);
|
||||
});
|
||||
|
||||
test('fillServers with configuration', () => {
|
||||
|
||||
const xml = new DOMParser().parseFromString("<servers/>");
|
||||
|
||||
process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';
|
||||
|
||||
settings.fillServers(xml, 'servers');
|
||||
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
|
||||
|
||||
expect(xmlStr).toBe(`<servers>
|
||||
<server>
|
||||
<id>id1</id>
|
||||
|
||||
|
||||
<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
|
||||
</server></servers>`);
|
||||
});
|
||||
|
||||
test('fillServers with configuration subLevel', () => {
|
||||
|
||||
const xml = new DOMParser().parseFromString("<servers/>");
|
||||
|
||||
process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": {"prop11": "value11", "prop12": "value12"}, "prop2": "value2"}}]';
|
||||
|
||||
settings.fillServers(xml, 'servers');
|
||||
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
|
||||
|
||||
expect(xmlStr).toBe(`<servers>
|
||||
<server>
|
||||
<id>id1</id>
|
||||
|
||||
|
||||
<configuration><prop1><prop11>value11</prop11><prop12>value12</prop12></prop1><prop2>value2</prop2></configuration>
|
||||
</server></servers>`);
|
||||
});
|
||||
|
||||
test('fillServers two servers', () => {
|
||||
|
||||
const xml = new DOMParser().parseFromString("<servers/>");
|
||||
@ -156,18 +233,20 @@ test('fillServers two servers', () => {
|
||||
|
||||
settings.fillServers(xml, 'servers');
|
||||
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml);
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');
|
||||
|
||||
expect(xmlStr).toBe(`<servers>
|
||||
<server>
|
||||
<id>id1</id>
|
||||
<username>username1</username>
|
||||
<password>password1</password>
|
||||
|
||||
</server>
|
||||
<server>
|
||||
<id>id2</id>
|
||||
<username>username2</username>
|
||||
<password>password2</password>
|
||||
|
||||
</server></servers>`);
|
||||
});
|
||||
|
||||
@ -184,7 +263,7 @@ test('fill servers incorrect fields', () => {
|
||||
expect(xmlStr).toBe('<servers/>');
|
||||
expect(consoleOutput).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringMatching(/::error::servers must contain id, username and password/)
|
||||
expect.stringMatching(/::error::servers must contain id, \(username and password\) or configuration/)
|
||||
])
|
||||
);
|
||||
});
|
||||
@ -232,13 +311,14 @@ test('fillServers github', () => {
|
||||
|
||||
settings.fillServerForGithub(xml);
|
||||
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml);
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');
|
||||
|
||||
expect(xmlStr).toBe(`<servers>
|
||||
<server>
|
||||
<id>github</id>
|
||||
<username>\${env.GITHUB_ACTOR}</username>
|
||||
<password>\${env.GITHUB_TOKEN}</password>
|
||||
|
||||
</server></servers>`);
|
||||
expect(consoleOutput).toEqual([]);
|
||||
});
|
||||
|
||||
@ -3,4 +3,5 @@
|
||||
<id />
|
||||
<username />
|
||||
<password />
|
||||
<configuration />
|
||||
</server>
|
||||
|
||||
Reference in New Issue
Block a user