Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

feature/default active profile #103

Merged
merged 3 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ Reference: [Maven Settings > Repositories](http://maven.apache.org/settings.html

Reference: [Maven Settings > Plugin Repositories](http://maven.apache.org/settings.html#Plugin_Repositories)


### `plugin_groups`
**Optional** json array of plugin groups to add to settings.xml

Expand All @@ -79,7 +78,17 @@ The `profile` element in the `settings.xml` is a truncated version of the `pom.x

Reference: [Maven Settings > Profiles](http://maven.apache.org/settings.html#profiles)

## Simple Usage

### `activeProfiles`
**Optional** json array of active profiles to add to settings.xml

Set of `activeProfile` elements, which each have a value of a `profile` `id`. Any `profile` `id` defined as an `activeProfile` will be active, regardless of any environment settings. If no matching profile is found nothing will happen. For example, if `env-test` is an `activeProfile`, a profile in a `pom.xml` (or `profile.xml`) with a corresponding `id` will be active. If no such profile is found then execution will continue as normal.

Reference: [Maven Settings > Active Profiles](https://maven.apache.org/settings.html#Active_Profiles)

---

## Basic Usage

````yaml
- name: maven-settings-xml-action
Expand All @@ -90,7 +99,7 @@ Reference: [Maven Settings > Profiles](http://maven.apache.org/settings.html#pro
servers: '[{ "id": "some-server", "username": "some.user", "password": "some.password" }]'
````

## Simple settings.xml
**Output**

````xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
Expand Down Expand Up @@ -131,6 +140,8 @@ Reference: [Maven Settings > Profiles](http://maven.apache.org/settings.html#pro
</settings>
````

----

## Full Usage

````yaml
Expand Down Expand Up @@ -205,10 +216,13 @@ Reference: [Maven Settings > Profiles](http://maven.apache.org/settings.html#pro
"some.plugin.group.id",
"some.other.plugin.group.id"
]

active_profiles: |
[
"some-profile"
]
````

## Full settings.xml
**Output**

````xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
Expand All @@ -217,7 +231,7 @@ Reference: [Maven Settings > Profiles](http://maven.apache.org/settings.html#pro
http://maven.apache.org/xsd/settings-1.0.0.xsd">

<activeProfiles>
<activeProfile>github</activeProfile>
<activeProfile>some-profile</activeProfile>
</activeProfiles>

<profiles>
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ inputs:
plugin_groups:
description: 'json array of plugin groups to add to settings.xml'
required: false
active_profiles:
description: 'json array of profile ids to add to settings.xml'
runs:
using: 'node12'
main: 'dist/index.js'
45 changes: 42 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3460,6 +3460,10 @@ function getDefaultRepositoryTemplate() {
return getTemplate('../template', 'default-repository.xml');
}

function getDefaultActiveProfileTemplate() {
return getTemplate('../template', 'default-active-profile.xml');
}

function getTemplate(filepath, filename) {
var templatePath = path.join(__dirname, filepath, filename);
var template = fs.readFileSync(templatePath).toString();
Expand Down Expand Up @@ -3490,6 +3494,7 @@ function writeSettings(settingsPath, templateXml) {
}

function update(templateXml) {
this.updateActiveProfiles(templateXml);
this.updateServers(templateXml);
this.updateMirrors(templateXml);
this.updateRepositories(templateXml);
Expand All @@ -3498,6 +3503,41 @@ function update(templateXml) {
this.updatePluginGroups(templateXml)
}

function updateActiveProfiles(templateXml) {

var activeProfilesInput = core.getInput('active_profiles');

if (!activeProfilesInput) {
applyDefaultActiveProfile(templateXml);
return;
}

var activeProfiles = JSON.parse(activeProfilesInput);

if (activeProfiles.length == 0) {
applyDefaultActiveProfile(templateXml);
return;
}

// apply custom repostories
activeProfiles.forEach((activeProfileInput) => {
activeProfileXml = templateXml.createElement("activeProfile");
activeProfileXml.textContent = activeProfileInput;
templateXml
.getElementsByTagName('activeProfiles')[0]
.appendChild(activeProfileXml);
});

}

function applyDefaultActiveProfile(templateXml) {
var defaultActiveProfile = getDefaultActiveProfileTemplate();

templateXml
.getElementsByTagName('activeProfiles')[0]
.appendChild(defaultActiveProfile);
}

function updateServers(templateXml) {
var serversInput = core.getInput('servers');

Expand Down Expand Up @@ -3707,18 +3747,17 @@ function objectToXml(obj) {

module.exports = {
getSettingsTemplate,
getDefaultRepositoryTemplate,
getTemplate,
formatSettings,
writeSettings,
update,
updateActiveProfiles,
updateServers,
updateMirrors,
updateRepositories,
updatePluginRepositories,
updateProfiles,
updatePluginGroups,
objectToXml
updatePluginGroups
}


Expand Down
1 change: 1 addition & 0 deletions dist/template/default-active-profile.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<activeProfile>github</activeProfile>
4 changes: 1 addition & 3 deletions dist/template/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">

<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<activeProfiles/>

<profiles>
<profile>
Expand Down
45 changes: 42 additions & 3 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function getDefaultRepositoryTemplate() {
return getTemplate('../template', 'default-repository.xml');
}

function getDefaultActiveProfileTemplate() {
return getTemplate('../template', 'default-active-profile.xml');
}

function getTemplate(filepath, filename) {
var templatePath = path.join(__dirname, filepath, filename);
var template = fs.readFileSync(templatePath).toString();
Expand Down Expand Up @@ -43,6 +47,7 @@ function writeSettings(settingsPath, templateXml) {
}

function update(templateXml) {
this.updateActiveProfiles(templateXml);
this.updateServers(templateXml);
this.updateMirrors(templateXml);
this.updateRepositories(templateXml);
Expand All @@ -51,6 +56,41 @@ function update(templateXml) {
this.updatePluginGroups(templateXml)
}

function updateActiveProfiles(templateXml) {

var activeProfilesInput = core.getInput('active_profiles');

if (!activeProfilesInput) {
applyDefaultActiveProfile(templateXml);
return;
}

var activeProfiles = JSON.parse(activeProfilesInput);

if (activeProfiles.length == 0) {
applyDefaultActiveProfile(templateXml);
return;
}

// apply custom repostories
activeProfiles.forEach((activeProfileInput) => {
activeProfileXml = templateXml.createElement("activeProfile");
activeProfileXml.textContent = activeProfileInput;
templateXml
.getElementsByTagName('activeProfiles')[0]
.appendChild(activeProfileXml);
});

}

function applyDefaultActiveProfile(templateXml) {
var defaultActiveProfile = getDefaultActiveProfileTemplate();

templateXml
.getElementsByTagName('activeProfiles')[0]
.appendChild(defaultActiveProfile);
}

function updateServers(templateXml) {
var serversInput = core.getInput('servers');

Expand Down Expand Up @@ -260,16 +300,15 @@ function objectToXml(obj) {

module.exports = {
getSettingsTemplate,
getDefaultRepositoryTemplate,
getTemplate,
formatSettings,
writeSettings,
update,
updateActiveProfiles,
updateServers,
updateMirrors,
updateRepositories,
updatePluginRepositories,
updateProfiles,
updatePluginGroups,
objectToXml
updatePluginGroups
}
1 change: 1 addition & 0 deletions template/default-active-profile.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<activeProfile>github</activeProfile>
4 changes: 1 addition & 3 deletions template/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">

<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<activeProfiles/>

<profiles>
<profile>
Expand Down
35 changes: 35 additions & 0 deletions test/resources/when-active-profiles-empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<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">

<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>

<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<name>Maven Central</name>
<url>https://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories/>
</profile>
</profiles>

<servers/>

<mirrors/>

<pluginGroups/>

</settings>
35 changes: 35 additions & 0 deletions test/resources/when-active-profiles-missing.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<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">

<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>

<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<name>Maven Central</name>
<url>https://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories/>
</profile>
</profiles>

<servers/>

<mirrors/>

<pluginGroups/>

</settings>
35 changes: 35 additions & 0 deletions test/resources/when-active-profiles-present.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<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">

<activeProfiles>
<activeProfile>foo-bar</activeProfile>
</activeProfiles>

<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<name>Maven Central</name>
<url>https://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories/>
</profile>
</profiles>

<servers/>

<mirrors/>

<pluginGroups/>

</settings>
Loading