Skip to content

Commit

Permalink
[MNG-5659] Provide project-specific settings located in ${session.roo…
Browse files Browse the repository at this point in the history
…tdir}/.mvn/settings.xml by default
  • Loading branch information
gnodet committed Apr 14, 2023
1 parent 8de0a56 commit 58d4e65
Show file tree
Hide file tree
Showing 16 changed files with 338 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 48,7 @@ public interface SettingsBuilder extends Service {
@Nonnull
default SettingsBuilderResult build(
@Nonnull Session session, @Nonnull Source globalSettingsSource, @Nonnull Source userSettingsSource) {
return build(SettingsBuilderRequest.build(session, globalSettingsSource, userSettingsSource));
return build(session, globalSettingsSource, null, userSettingsSource);
}

/**
Expand All @@ -60,6 60,37 @@ default SettingsBuilderResult build(
@Nonnull
default SettingsBuilderResult build(
@Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) {
return build(SettingsBuilderRequest.build(session, globalSettingsPath, userSettingsPath));
return build(session, globalSettingsPath, null, userSettingsPath);
}

/**
* Builds the effective settings of the specified settings sources.
*
* @return the result of the settings building, never {@code null}
* @throws SettingsBuilderException if the effective settings could not be built
*/
@Nonnull
default SettingsBuilderResult build(
@Nonnull Session session,
@Nonnull Source globalSettingsSource,
@Nonnull Source projectSettingsSource,
@Nonnull Source userSettingsSource) {
return build(
SettingsBuilderRequest.build(session, globalSettingsSource, projectSettingsSource, userSettingsSource));
}

/**
* Builds the effective settings of the specified settings paths.
*
* @return the result of the settings building, never {@code null}
* @throws SettingsBuilderException if the effective settings could not be built
*/
@Nonnull
default SettingsBuilderResult build(
@Nonnull Session session,
@Nonnull Path globalSettingsPath,
@Nonnull Path projectSettingsPath,
@Nonnull Path userSettingsPath) {
return build(SettingsBuilderRequest.build(session, globalSettingsPath, projectSettingsPath, userSettingsPath));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 56,22 @@ public interface SettingsBuilderRequest {
@Nonnull
Optional<Source> getGlobalSettingsSource();

/**
* Gets the project settings source.
*
* @return the project settings source or {@code null} if none
*/
@Nonnull
Optional<Source> getProjectSettingsSource();

/**
* Gets the project settings path.
*
* @return the project settings path or {@code null} if none
*/
@Nonnull
Optional<Path> getProjectSettingsPath();

/**
* Gets the user settings path.
*
Expand All @@ -75,19 91,39 @@ public interface SettingsBuilderRequest {
@Nonnull
static SettingsBuilderRequest build(
@Nonnull Session session, @Nonnull Source globalSettingsSource, @Nonnull Source userSettingsSource) {
return build(session, globalSettingsSource, null, userSettingsSource);
}

@Nonnull
static SettingsBuilderRequest build(
@Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) {
return build(session, globalSettingsPath, null, userSettingsPath);
}

@Nonnull
static SettingsBuilderRequest build(
@Nonnull Session session,
@Nonnull Source globalSettingsSource,
@Nonnull Source projectSettingsSource,
@Nonnull Source userSettingsSource) {
return builder()
.session(nonNull(session, "session cannot be null"))
.globalSettingsSource(nonNull(globalSettingsSource, "globalSettingsSource cannot be null"))
.projectSettingsSource(nonNull(projectSettingsSource, "projectSettingsSource cannot be null"))
.userSettingsSource(nonNull(userSettingsSource, "userSettingsSource cannot be null"))
.build();
}

@Nonnull
static SettingsBuilderRequest build(
@Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) {
@Nonnull Session session,
@Nonnull Path globalSettingsPath,
@Nonnull Path projectSettingsPath,
@Nonnull Path userSettingsPath) {
return builder()
.session(nonNull(session, "session cannot be null"))
.globalSettingsPath(nonNull(globalSettingsPath, "globalSettingsPath cannot be null"))
.projectSettingsPath(nonNull(projectSettingsPath, "projectSettingsPath cannot be null"))
.userSettingsPath(nonNull(userSettingsPath, "userSettingsPath cannot be null"))
.build();
}
Expand All @@ -102,6 138,8 @@ class SettingsBuilderRequestBuilder {
Session session;
Path globalSettingsPath;
Source globalSettingsSource;
Path projectSettingsPath;
Source projectSettingsSource;
Path userSettingsPath;
Source userSettingsSource;

Expand All @@ -120,6 158,16 @@ public SettingsBuilderRequestBuilder globalSettingsSource(Source globalSettingsS
return this;
}

public SettingsBuilderRequestBuilder projectSettingsPath(Path projectSettingsPath) {
this.projectSettingsPath = projectSettingsPath;
return this;
}

public SettingsBuilderRequestBuilder projectSettingsSource(Source projectSettingsSource) {
this.projectSettingsSource = projectSettingsSource;
return this;
}

public SettingsBuilderRequestBuilder userSettingsPath(Path userSettingsPath) {
this.userSettingsPath = userSettingsPath;
return this;
Expand All @@ -132,12 180,20 @@ public SettingsBuilderRequestBuilder userSettingsSource(Source userSettingsSourc

public SettingsBuilderRequest build() {
return new DefaultSettingsBuilderRequest(
session, globalSettingsPath, globalSettingsSource, userSettingsPath, userSettingsSource);
session,
globalSettingsPath,
globalSettingsSource,
projectSettingsPath,
projectSettingsSource,
userSettingsPath,
userSettingsSource);
}

private static class DefaultSettingsBuilderRequest extends BaseRequest implements SettingsBuilderRequest {
private final Path globalSettingsPath;
private final Source globalSettingsSource;
private final Path projectSettingsPath;
private final Source projectSettingsSource;
private final Path userSettingsPath;
private final Source userSettingsSource;

Expand All @@ -146,11 202,15 @@ private static class DefaultSettingsBuilderRequest extends BaseRequest implement
@Nonnull Session session,
@Nullable Path globalSettingsPath,
@Nullable Source globalSettingsSource,
@Nullable Path projectSettingsPath,
@Nullable Source projectSettingsSource,
@Nullable Path userSettingsPath,
@Nullable Source userSettingsSource) {
super(session);
this.globalSettingsPath = globalSettingsPath;
this.globalSettingsSource = globalSettingsSource;
this.projectSettingsPath = projectSettingsPath;
this.projectSettingsSource = projectSettingsSource;
this.userSettingsPath = userSettingsPath;
this.userSettingsSource = userSettingsSource;
}
Expand All @@ -167,6 227,18 @@ public Optional<Source> getGlobalSettingsSource() {
return Optional.ofNullable(globalSettingsSource);
}

@Nonnull
@Override
public Optional<Path> getProjectSettingsPath() {
return Optional.ofNullable(projectSettingsPath);
}

@Nonnull
@Override
public Optional<Source> getProjectSettingsSource() {
return Optional.ofNullable(projectSettingsSource);
}

@Nonnull
@Override
public Optional<Path> getUserSettingsPath() {
Expand Down
5 changes: 3 additions & 2 deletions api/maven-api-settings/src/main/mdo/settings.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 53,7 @@
<code>
<![CDATA[
public static final String USER_LEVEL = "user-level";
public static final String PROJECT_LEVEL = "project-level";
public static final String GLOBAL_LEVEL = "global-level";
private String sourceLevel = USER_LEVEL;
Expand All @@ -64,9 65,9 @@
{
throw new IllegalStateException( "Cannot reset sourceLevel attribute; it is already set to: " sourceLevel );
}
else if ( !( USER_LEVEL.equals( sourceLevel ) || GLOBAL_LEVEL.equals( sourceLevel ) ) )
else if ( !( USER_LEVEL.equals( sourceLevel ) || PROJECT_LEVEL.equals( sourceLevel ) || GLOBAL_LEVEL.equals( sourceLevel ) ) )
{
throw new IllegalArgumentException( "sourceLevel must be one of: {" USER_LEVEL "," GLOBAL_LEVEL "}" );
throw new IllegalArgumentException( "sourceLevel must be one of: {" USER_LEVEL "," PROJECT_LEVEL "," GLOBAL_LEVEL "}" );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 90,8 @@ public class DefaultMavenExecutionRequest implements MavenExecutionRequest {

private File userSettingsFile;

private File projectSettingsFile;

private File globalSettingsFile;

private File userToolchainsFile;
Expand Down Expand Up @@ -842,6 844,18 @@ public MavenExecutionRequest setUserSettingsFile(File userSettingsFile) {
return this;
}

@Override
public File getProjectSettingsFile() {
return projectSettingsFile;
}

@Override
public MavenExecutionRequest setProjectSettingsFile(File projectSettingsFile) {
this.projectSettingsFile = projectSettingsFile;

return this;
}

@Override
public File getGlobalSettingsFile() {
return globalSettingsFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 393,10 @@ public interface MavenExecutionRequest {

MavenExecutionRequest setUserSettingsFile(File userSettingsFile);

File getProjectSettingsFile();

MavenExecutionRequest setProjectSettingsFile(File projectSettingsFile);

File getGlobalSettingsFile();

MavenExecutionRequest setGlobalSettingsFile(File globalSettingsFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 88,7 @@ private Settings build(SettingsBuildingRequest request) throws IOException, XmlP
public Settings buildSettings(MavenExecutionRequest request) throws IOException, XmlPullParserException {
SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
settingsRequest.setUserSettingsFile(request.getUserSettingsFile());
settingsRequest.setProjectSettingsFile(request.getProjectSettingsFile());
settingsRequest.setGlobalSettingsFile(request.getGlobalSettingsFile());
settingsRequest.setUserProperties(request.getUserProperties());
settingsRequest.setSystemProperties(request.getSystemProperties());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 74,8 @@ public class CLIManager {

public static final char ALTERNATE_USER_SETTINGS = 's';

public static final String ALTERNATE_PROJECT_SETTINGS = "ps";

public static final String ALTERNATE_GLOBAL_SETTINGS = "gs";

public static final char ALTERNATE_USER_TOOLCHAINS = 't';
Expand Down Expand Up @@ -192,6 194,11 @@ public CLIManager() {
.desc("Alternate path for the user settings file")
.hasArg()
.build());
options.addOption(Option.builder(ALTERNATE_PROJECT_SETTINGS)
.longOpt("project-settings")
.desc("Alternate path for the project settings file")
.hasArg()
.build());
options.addOption(Option.builder(ALTERNATE_GLOBAL_SETTINGS)
.longOpt("global-settings")
.desc("Alternate path for the global settings file")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 110,12 @@ public MavenExecutionRequest getRequest() {
public void setUserProperties(Properties properties) {
this.userProperties.putAll(properties);
}

public Path getRootdir() {
return rootdir;
}

public Path getTopdir() {
return topdir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 66,8 @@ public class SettingsXmlConfigurationProcessor implements ConfigurationProcessor

public static final File DEFAULT_USER_SETTINGS_FILE = new File(USER_MAVEN_CONFIGURATION_HOME, "settings.xml");

public static final File DEFAULT_PROJECT_SETTINGS_FILE = new File(".mvn", "settings.xml");

public static final File DEFAULT_GLOBAL_SETTINGS_FILE = new File(System.getProperty("maven.conf"), "settings.xml");

private static final Logger LOGGER = LoggerFactory.getLogger(SettingsXmlConfigurationProcessor.class);
Expand Down Expand Up @@ -98,6 100,24 @@ public void process(CliRequest cliRequest) throws Exception {
userSettingsFile = DEFAULT_USER_SETTINGS_FILE;
}

File projectSettingsFile;

if (commandLine.hasOption(CLIManager.ALTERNATE_PROJECT_SETTINGS)) {
projectSettingsFile = new File(commandLine.getOptionValue(CLIManager.ALTERNATE_PROJECT_SETTINGS));
projectSettingsFile = resolveFile(projectSettingsFile, workingDirectory);

if (!projectSettingsFile.isFile()) {
throw new FileNotFoundException(
"The specified project settings file does not exist: " projectSettingsFile);
}
} else if (cliRequest.getRootdir() != null) {
projectSettingsFile = DEFAULT_PROJECT_SETTINGS_FILE;
projectSettingsFile =
resolveFile(projectSettingsFile, cliRequest.getRootdir().toString());
} else {
projectSettingsFile = null;
}

File globalSettingsFile;

if (commandLine.hasOption(CLIManager.ALTERNATE_GLOBAL_SETTINGS)) {
Expand All @@ -113,10 133,12 @@ public void process(CliRequest cliRequest) throws Exception {
}

request.setGlobalSettingsFile(globalSettingsFile);
request.setProjectSettingsFile(projectSettingsFile);
request.setUserSettingsFile(userSettingsFile);

SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
settingsRequest.setGlobalSettingsFile(globalSettingsFile);
settingsRequest.setProjectSettingsFile(projectSettingsFile);
settingsRequest.setUserSettingsFile(userSettingsFile);
settingsRequest.setSystemProperties(cliRequest.getSystemProperties());
settingsRequest.setUserProperties(cliRequest.getUserProperties());
Expand All @@ -128,6 150,9 @@ public void process(CliRequest cliRequest) throws Exception {
LOGGER.debug(
"Reading global settings from '{}'",
getLocation(settingsRequest.getGlobalSettingsSource(), settingsRequest.getGlobalSettingsFile()));
LOGGER.debug(
"Reading project settings from '{}'",
getLocation(settingsRequest.getProjectSettingsSource(), settingsRequest.getProjectSettingsFile()));
LOGGER.debug(
"Reading user settings from '{}'",
getLocation(settingsRequest.getUserSettingsSource(), settingsRequest.getUserSettingsFile()));
Expand Down
8 changes: 7 additions & 1 deletion maven-settings-builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 78,13 @@ under the License.
<plugin>
<groupId>com.github.siom79.japicmp</groupId>
<artifactId>japicmp-maven-plugin</artifactId>
<configuration />
<configuration>
<parameter>
<excludes>
<exclude>org.apache.maven.settings.validation.SettingsValidator#validate(org.apache.maven.settings.Settings,boolean,org.apache.maven.settings.building.SettingsProblemCollector):METHOD_NEW_DEFAULT</exclude>
</excludes>
</parameter>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Loading

0 comments on commit 58d4e65

Please sign in to comment.