Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add includeEnvironment parameter #870 #871

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ mvn -f pom.lockfile.xml
- `reduced` will reduce the lockfile only containing the dependencies after dependency resolution conflicts are resolved. This format is smaller, and easier to review and read. Only use this if you do not need the full dependency tree.
- `includeMavenPlugins` will include the maven plugins in the lockfile. This is useful if you want to validate the Maven plugins as well.
- `allowValidationFailure` (default=false) allow validation failures, printing a warning instead of an error. This is useful if you want to only validate the Maven lockfile, but do not need to fail the build in case the lockfile is not valid. Use with caution, you loose all guarantees.
- `includeEnvironment` will include the environment metadata in the lockfile. This is useful if you want to have warnings when the environment changes.
- `checksumAlgorithm` will set the checksum algorithm used to generate the lockfile. The default depends on your checksum mode.
- `checksumMode` will set the checksum mode used to generate the lockfile. See [Checksum Modes](/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/checksum/ChecksumModes.java) for more information.
- `skip` will skip the execution of the plugin. This is useful if you would like to disable the plugin for a specific module.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ public abstract class AbstractLockfileMojo extends AbstractMojo {
@Component
protected DependencyResolver dependencyResolver;

@Parameter(property = "includeMavenPlugins")
@Parameter(property = "includeMavenPlugins", defaultValue = "false")
protected String includeMavenPlugins;

@Parameter(property = "allowValidationFailure", defaultValue = "false")
protected String allowValidationFailure;

@Parameter(property = "includeEnvironment", defaultValue = "true")
protected String includeEnvironment;

@Parameter(defaultValue = "${maven.version}")
protected String mavenVersion;

Expand Down Expand Up @@ -100,6 +103,7 @@ protected Config getConfig() {
return new Config(
Boolean.parseBoolean(includeMavenPlugins),
Boolean.parseBoolean(allowValidationFailure),
Boolean.parseBoolean(includeEnvironment),
Boolean.parseBoolean(reduced),
mojo.getPlugin().getVersion(),
chosenMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class GenerateLockFileMojo extends AbstractLockfileMojo {

@Parameter(defaultValue = "true", property = "getConfigFromFile")
String getConfigFromFile;

/**
* Generate a lock file for the dependencies of the current project.
* @throws MojoExecutionException if the lock file could not be written or the generation failed.
Expand All @@ -40,10 +41,13 @@ public void execute() throws MojoExecutionException {
getLog().info("Skipping maven-lockfile");
}
try {
Environment environment = generateMetaInformation();
LockFile lockFileFromFile =
Files.exists(getLockFilePath(project)) ? LockFile.readLockFile(getLockFilePath(project)) : null;
Config config = Boolean.parseBoolean(getConfigFromFile) ? getConfig(lockFileFromFile) : getConfig();
Environment environment = null;
if (config.isIncludeEnvironment()) {
environment = generateMetaInformation();
}
MetaData metaData = new MetaData(environment, config);

if (lockFileFromFile == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public void execute() throws MojoExecutionException {

LockFile lockFileFromFile = LockFile.readLockFile(getLockFilePath(project));
Config config = lockFileFromFile.getConfig() == null ? getConfig() : lockFileFromFile.getConfig();
if (lockFileFromFile.getConfig() == null) {
getLog().warn("No config was found in the lock file. Using default config.");
}
MetaData metaData = new MetaData(environment, config);
getLog().warn("No config was found in the lock file. Using default config.");
AbstractChecksumCalculator checksumCalculator = getChecksumCalculator(config);
LockFile lockFileFromProject = LockFileFacade.generateLockFileFromProject(
session, project, dependencyCollectorBuilder, checksumCalculator, metaData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Config {

private final boolean includeMavenPlugins;
private final boolean allowValidationFailure;
private final boolean includeEnvironment;
private final boolean reduced;
private final String mavenLockfileVersion;
private final String checksumMode;
Expand All @@ -15,12 +16,14 @@ public class Config {
public Config(
boolean includeMavenPlugins,
boolean allowValidationFailure,
boolean includeEnvironment,
boolean reduced,
String mavenLockfileVersion,
String checksumMode,
String checksumAlgorithm) {
this.includeMavenPlugins = includeMavenPlugins;
this.allowValidationFailure = allowValidationFailure;
this.includeEnvironment = includeEnvironment;
this.reduced = reduced;
this.mavenLockfileVersion = mavenLockfileVersion;
this.checksumMode = checksumMode;
Expand All @@ -30,6 +33,7 @@ public Config(
public Config() {
this.includeMavenPlugins = false;
this.allowValidationFailure = false;
this.includeEnvironment = true;
this.reduced = false;
this.mavenLockfileVersion = "1";
this.checksumMode = ChecksumModes.MAVEN_LOCAL.name();
Expand All @@ -47,6 +51,12 @@ public boolean isIncludeMavenPlugins() {
public boolean isAllowValidationFailure() {
return allowValidationFailure;
}
/**
* @return the includeEnvironment
*/
public boolean isIncludeEnvironment() {
return includeEnvironment;
}
/**
* @return the reduced
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LockfileTest {
@Test
void shouldLockFilesEqualWhenOrderIsChanged() {
var metadata = new MetaData(
new Environment("os", "mv", "jv"), new Config(true, false, false, "1", "maven_local", "sha1"));
new Environment("os", "mv", "jv"), new Config(true, false, true, false, "1", "maven_local", "sha1"));
var groupId = GroupId.of("g");
var artifactId = ArtifactId.of("a");
var version = VersionNumber.of("a");
Expand Down
46 changes: 46 additions & 0 deletions maven_plugin/src/test/java/it/IntegrationTestsIT.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it;

import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
massimeddu-sj marked this conversation as resolved.
Show resolved Hide resolved
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.soebes.itf.jupiter.extension.MavenJupiterExtension;
Expand Down Expand Up @@ -44,6 +45,7 @@ public void singleDependency(MavenExecutionResult result) throws Exception {
Path lockFilePath = findFile(result, "lockfile.json");
assertThat(lockFilePath).exists();
var lockFile = LockFile.readLockFile(lockFilePath);
assertThat(lockFile.getEnvironment()).isNotNull();
assertThat(lockFile.getDependencies()).hasSize(1);
var junitDep = lockFile.getDependencies().toArray(DependencyNode[]::new)[0];
assertThat(junitDep.getArtifactId()).extracting(ArtifactId::getValue).isEqualTo("spoon-core");
Expand Down Expand Up @@ -270,4 +272,48 @@ public void singleDependencyCheckMustWarn(MavenExecutionResult result) throws Ex
// we changed the group id of "groupId": "org.opentest4j", to "groupId": "org.opentest4j5",
assertThat(result).isSuccessful();
}

@MavenTest
public void withEnvironment(MavenExecutionResult result) throws Exception {
// contract: a null environment should be returned if include environment is false
assertThat(result).isSuccessful();
Path lockFilePath = findFile(result, "lockfile.json");
assertThat(lockFilePath).exists();
var lockFile = LockFile.readLockFile(lockFilePath);
assertThat(lockFile.getConfig().isIncludeEnvironment()).isTrue();
assertThat(lockFile.getEnvironment()).isNotNull();
}

@MavenTest
public void withoutEnvironment(MavenExecutionResult result) throws Exception {
// contract: a not null environment should be returned if include environment is true
assertThat(result).isSuccessful();
Path lockFilePath = findFile(result, "lockfile.json");
assertThat(lockFilePath).exists();
var lockFile = LockFile.readLockFile(lockFilePath);
assertThat(lockFile.getConfig().isIncludeEnvironment()).isFalse();
assertThat(lockFile.getEnvironment()).isNull();
}

@MavenTest
public void withEnvironmentFromLockfile(MavenExecutionResult result) throws Exception {
// contract: a not null environment should be returned if include environment is true
assertThat(result).isSuccessful();
Path lockFilePath = findFile(result, "lockfile.json");
assertThat(lockFilePath).exists();
var lockFile = LockFile.readLockFile(lockFilePath);
assertThat(lockFile.getConfig().isIncludeEnvironment()).isTrue();
assertThat(lockFile.getEnvironment()).isNotNull();
}

@MavenTest
public void withoutEnvironmentFromLockfile(MavenExecutionResult result) throws Exception {
// contract: a null environment should be returned if include environment is false
assertThat(result).isSuccessful();
Path lockFilePath = findFile(result, "lockfile.json");
assertThat(lockFilePath).exists();
var lockFile = LockFile.readLockFile(lockFilePath);
assertThat(lockFile.getConfig().isIncludeEnvironment()).isFalse();
assertThat(lockFile.getEnvironment()).isNull();
}
}
Loading
Loading