Skip to content

Commit c56ba51

Browse files
committed
Extended settings.xml - Profiles with Repo
1 parent f4f1212 commit c56ba51

4 files changed

Lines changed: 150 additions & 4 deletions

File tree

action.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ inputs:
5353
description: 'Environment variable name for the GPG private key passphrase. Default is
5454
$GPG_PASSPHRASE.'
5555
required: false
56+
repo-id:
57+
description: 'Identifier of a Named Repo - e.g. "github"'
58+
required: false
59+
repo-url:
60+
description: 'URL of a repository where maven will look for Dependencies - e.g. "https://maven.pkg.github.com/<USERNAME_or_ORGANIZATION>/*"'
61+
required: false
62+
no-snapshots:
63+
description: 'Determines whether snapshots for custom repositories are allowed; defaults to allowing snapshots.'
64+
required: false
65+
default: false
66+
use-central:
67+
description: 'Sets the Flag, whether to use Maven-Central or not. (default allows Central repo)'
68+
required: false
69+
default: true
70+
prioritize-central:
71+
description: 'Allows it to define, which Repo will be chosen first to download Dependencies. (default Central prior Custom)'
72+
required: false
73+
default: true
5674
cache:
5775
description: 'Name of the build platform to cache dependencies. It can be "maven", "gradle" or "sbt".'
5876
required: false

docs/advanced-usage.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,65 @@ See the help docs on [Publishing a Package](https://help.github.com/en/github/ma
436436

437437
***NOTE***: If the error that states, `gpg: Sorry, no terminal at all requested - can't get input` [is encountered](https://github.com/actions/setup-java/issues/554), please update the version of `maven-gpg-plugin` to 1.6 or higher.
438438

439+
## Resolving Dependencies
440+
441+
If you use setup-java action to build your project with dependencies of another repository then Maven Central, you need to tell maven where to find your Dependencies.
442+
443+
444+
```yaml
445+
- name: Set up Apache Maven Central
446+
uses: actions/setup-java@v4
447+
with:
448+
distribution: 'temurin'
449+
java-version: '11'
450+
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
451+
server-username: ${{ secrets.USERNAME }}
452+
server-password: ${{ secrets.PASS_WORD }}
453+
repo-id: github
454+
repo-url: 'https://maven.pkg.github.com/<USERNAME_or_ORGANIZATION>/*'
455+
no-snapshots: false # (optional) default Snapshots enabled true
456+
use-central: true # (optional) default uses Central
457+
prioritize-central: true # (optional) default first lookup Maven Central
458+
```
459+
The generated `settings.xml` will look like:
460+
461+
```xml
462+
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
463+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
464+
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
465+
<activeProfiles>
466+
<activeProfile>github</activeProfile>
467+
</activeProfiles>
468+
<profiles>
469+
<profile>
470+
<id>github</id>
471+
<repositories>
472+
<repository>
473+
<id>central</id>
474+
<url>https://repo1.maven.org/maven2</url>
475+
</repository>
476+
<repository>
477+
<id>github</id>
478+
<url>https://maven.pkg.github.com/<USERNAME_or_ORGANIZATION>/*</url>
479+
<snapshots>
480+
<!--
481+
<enabled>true</enabled>
482+
-->
483+
</snapshots>
484+
</repository>
485+
</repositories>
486+
</profile>
487+
</profiles>
488+
<servers>
489+
<server>
490+
<id>github</id>
491+
<username>${secrets.USERNAME}</username>
492+
<password>${secrets.PASS_WORD}</password>
493+
</server>
494+
</servers>
495+
</settings>
496+
```
497+
439498
## Apache Maven with a settings path
440499

441500
When using an Actions self-hosted runner with multiple shared runners the default `$HOME` directory can be shared by a number runners at the same time which could overwrite existing settings file. Setting the `settings-path` variable allows you to choose a unique location for your settings file.

src/auth.ts

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,27 @@ export async function configureAuthentication() {
3131
if (gpgPrivateKey) {
3232
core.setSecret(gpgPrivateKey);
3333
}
34+
const repoId = core.getInput(constants.INPUT_REPO_ID);
35+
const repoUrl = core.getInput(constants.INPUT_REPO_URL);
36+
const useCentral = core.getBooleanInput(constants.INPUT_USE_CENTRAL);
37+
const prioritizeCentral = core.getBooleanInput(
38+
constants.INPUT_PRIORITIZE_CENTRAL
39+
);
40+
const noSnapshots = core.getBooleanInput(constants.INPUT_REPO_NO_SNAPSHOTS);
3441

3542
await createAuthenticationSettings(
3643
id,
3744
username,
3845
password,
3946
settingsDirectory,
4047
overwriteSettings,
41-
gpgPassphrase
48+
gpgPassphrase,
49+
repoId,
50+
undefined, // profileId
51+
repoUrl,
52+
useCentral,
53+
prioritizeCentral,
54+
noSnapshots
4255
);
4356

4457
if (gpgPrivateKey) {
@@ -54,15 +67,35 @@ export async function createAuthenticationSettings(
5467
password: string,
5568
settingsDirectory: string,
5669
overwriteSettings: boolean,
57-
gpgPassphrase: string | undefined = undefined
70+
gpgPassphrase: string | undefined = undefined,
71+
repoId?: string,
72+
profileId: string | undefined = repoId, // simplifying fallback (entrypoint for multi-profile)
73+
repoUrl?: string,
74+
useCentral?: boolean,
75+
prioritizeCentral?: boolean,
76+
noSnapshots?: boolean
5877
) {
5978
core.info(`Creating ${constants.MVN_SETTINGS_FILE} with server-id: ${id}`);
79+
if (profileId) {
80+
core.info(`Using [${profileId}] to add Dependencies from [${repoUrl}]`);
81+
}
6082
// when an alternate m2 location is specified use only that location (no .m2 directory)
6183
// otherwise use the home/.m2/ path
6284
await io.mkdirP(settingsDirectory);
6385
await write(
6486
settingsDirectory,
65-
generate(id, username, password, gpgPassphrase),
87+
generate(
88+
id,
89+
username,
90+
password,
91+
gpgPassphrase,
92+
repoId,
93+
profileId,
94+
repoUrl,
95+
useCentral,
96+
prioritizeCentral,
97+
noSnapshots
98+
),
6699
overwriteSettings
67100
);
68101
}
@@ -72,14 +105,45 @@ export function generate(
72105
id: string,
73106
username: string,
74107
password: string,
75-
gpgPassphrase?: string | undefined
108+
gpgPassphrase?: string | undefined,
109+
repoId?: string,
110+
profileId?: string,
111+
repoUrl?: string,
112+
useCentral: boolean = true,
113+
prioritizeCentral: boolean = true,
114+
noSnapshots: boolean = false
76115
) {
116+
const centralRepo = {
117+
repository: {
118+
id: 'central',
119+
url: 'https://repo1.maven.org/maven2'
120+
}
121+
};
122+
const customRepo = {
123+
repository: {
124+
id: repoId,
125+
url: repoUrl,
126+
...(noSnapshots ? {snapshots: {enabled: false}} : {})
127+
}
128+
};
129+
const profiles = {
130+
profile: {
131+
id: profileId,
132+
repositories: useCentral
133+
? prioritizeCentral
134+
? [centralRepo, customRepo] // faster if more deps from central
135+
: [customRepo, centralRepo]
136+
: [customRepo] // to exclude central
137+
}
138+
};
77139
const xmlObj: {[key: string]: any} = {
78140
settings: {
79141
'@xmlns': 'http://maven.apache.org/SETTINGS/1.0.0',
80142
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
81143
'@xsi:schemaLocation':
82144
'http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd',
145+
activeProfiles: profileId ? [{activeProfile: profileId}] : [],
146+
profiles: repoId && profileId && repoUrl ? [profiles] : [],
83147
servers: {
84148
server: [
85149
{

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export const INPUT_SETTINGS_PATH = 'settings-path';
1313
export const INPUT_OVERWRITE_SETTINGS = 'overwrite-settings';
1414
export const INPUT_GPG_PRIVATE_KEY = 'gpg-private-key';
1515
export const INPUT_GPG_PASSPHRASE = 'gpg-passphrase';
16+
export const INPUT_REPO_ID = 'repo-id';
17+
export const INPUT_REPO_URL = 'repo-url';
18+
export const INPUT_REPO_NO_SNAPSHOTS = 'no-snapshots';
19+
export const INPUT_USE_CENTRAL = 'use-central';
20+
export const INPUT_PRIORITIZE_CENTRAL = 'prioritize-central';
1621

1722
export const INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined;
1823
export const INPUT_DEFAULT_GPG_PASSPHRASE = 'GPG_PASSPHRASE';

0 commit comments

Comments
 (0)