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 parameter to change lockfile name #1026

Merged
merged 4 commits into from
Dec 19, 2024
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -73,6 +73,7 @@ mvn -f pom.lockfile.xml
- `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.
- `lockfileName` (default="lockfile.json") will set the name of the lockfile file to be generated/read.
- `getConfigFromFile` will read the configuration of maven lockfile from the existing lockfile.
## Format

Original file line number Diff line number Diff line change
@@ -65,6 +65,9 @@ public abstract class AbstractLockfileMojo extends AbstractMojo {
@Parameter(defaultValue = "false", property = "skip")
protected String skip;

@Parameter(defaultValue = "lockfile.json", property = "lockfileName")
protected String lockfileName;

@Parameter(defaultValue = "${mojoExecution}", readonly = true)
protected MojoExecution mojo;

Original file line number Diff line number Diff line change
@@ -45,6 +45,9 @@ public class FreezeDependencyMojo extends AbstractMojo {
@Parameter(defaultValue = "pom.lockfile.xml", property = "pomLockfileOutput")
private String pomLockfileOutput;

@Parameter(defaultValue = "lockfile.json", property = "lockfileName")
private String lockfileName;

/**
* Freezes the dependencies of the project. Every dependency will be locked to a specific version.
*
@@ -54,7 +57,7 @@ public void execute() throws MojoExecutionException {
File pomFile = project.getFile();
File pomLockFile = new File(project.getBasedir(), pomLockfileOutput);
try {
LockFile lockFile = LockFile.readLockFile(LockFileFacade.getLockFilePath(project));
LockFile lockFile = LockFile.readLockFile(LockFileFacade.getLockFilePath(project, lockfileName));
List<Dependency> filteredDependencies = getNearestVersionDependency(lockFile);
Model pomModel = readPomFile(pomFile);
updateDependencies(pomModel, filteredDependencies);
Original file line number Diff line number Diff line change
@@ -42,8 +42,9 @@ public void execute() throws MojoExecutionException {
return;
}
try {
LockFile lockFileFromFile =
Files.exists(getLockFilePath(project)) ? LockFile.readLockFile(getLockFilePath(project)) : null;
LockFile lockFileFromFile = Files.exists(getLockFilePath(project, lockfileName))
? LockFile.readLockFile(getLockFilePath(project, lockfileName))
: null;
Config config = Boolean.parseBoolean(getConfigFromFile) ? getConfig(lockFileFromFile) : getConfig();
Environment environment = null;
if (config.isIncludeEnvironment()) {
@@ -58,7 +59,7 @@ public void execute() throws MojoExecutionException {
LockFile lockFile = LockFileFacade.generateLockFileFromProject(
session, project, dependencyCollectorBuilder, checksumCalculator, metaData);

Path lockFilePath = LockFileFacade.getLockFilePath(project);
Path lockFilePath = LockFileFacade.getLockFilePath(project, lockfileName);
Files.writeString(lockFilePath, JsonUtils.toJson(lockFile));
getLog().info("Lockfile written to " + lockFilePath);
} catch (IOException e) {
Original file line number Diff line number Diff line change
@@ -61,8 +61,8 @@ public boolean endVisit(DependencyNode node) {
* @param project The project to generate a lock file for.
* @return A lock file for the project.
*/
public static Path getLockFilePath(MavenProject project) {
return Path.of(project.getBasedir().getAbsolutePath(), "lockfile.json");
public static Path getLockFilePath(MavenProject project, String lockfileName) {
return Path.of(project.getBasedir().getAbsolutePath(), lockfileName);
}

private LockFileFacade() {
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ public void execute() throws MojoExecutionException {
getLog().info("Validating lock file ...");
Environment environment = generateMetaInformation();

LockFile lockFileFromFile = LockFile.readLockFile(getLockFilePath(project));
LockFile lockFileFromFile = LockFile.readLockFile(getLockFilePath(project, lockfileName));
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.");
47 changes: 41 additions & 6 deletions maven_plugin/src/test/java/it/IntegrationTestsIT.java
Original file line number Diff line number Diff line change
@@ -152,6 +152,15 @@ private Path findFile(MavenExecutionResult result, String fileName) throws IOExc
.orElseThrow(FileNotFoundException::new);
}

private boolean fileExists(MavenExecutionResult result, String fileName) throws IOException {
return Files.find(
result.getMavenProjectResult().getTargetBaseDirectory(),
Integer.MAX_VALUE,
(path, attr) -> path.getFileName().toString().contains(fileName))
.findAny()
.isPresent();
}

private Model readPom(Path pomPath) throws IOException, XmlPullParserException {
try (Reader reader = Files.newBufferedReader(pomPath)) {
MavenXpp3Reader pomReader = new MavenXpp3Reader();
@@ -336,12 +345,38 @@ public void orderedLockfile(MavenExecutionResult result) throws Exception {
public void skipLockfile(MavenExecutionResult result) throws Exception {
// contract: the lockfile should not be generated if skip option is true
assertThat(result).isSuccessful();
var fileExists = Files.find(
result.getMavenProjectResult().getTargetBaseDirectory(),
Integer.MAX_VALUE,
(path, attr) -> path.getFileName().toString().contains("lockfile.json"))
.findAny()
.isPresent();
var fileExists = fileExists(result, "lockfile.json");
assertThat(fileExists).isFalse();
}

@MavenTest
public void differentLockfileName(MavenExecutionResult result) throws Exception {
// contract: the lockfile should be generated with a different name
assertThat(result).isSuccessful();
var lockfileExists = fileExists(result, "lockfile.json");
assertThat(lockfileExists).isFalse();
var differentLockfileNameExists = fileExists(result, "different-lockfile-name.json");
assertThat(differentLockfileNameExists).isTrue();
}

@MavenTest
public void differentLockfileNameFreezeShouldSucceed(MavenExecutionResult result) throws Exception {
// contract: if there exists a different-name-lockfile.json and -DlockfileName="different-lockfile-name.json" is
// provided, freeze should succeed
assertThat(result).isSuccessful();
}

@MavenTest
public void differentLockfileNameValidateShouldFail(MavenExecutionResult result) throws Exception {
// contract: if there exists a lockfile.json but -DlockfileName="different-lockfile-name.json" is provided,
// validate should fail
assertThat(result).isFailure();
}

@MavenTest
public void differentLockfileNameValidateShouldSucceed(MavenExecutionResult result) throws Exception {
// contract: if there exists a different-name-lockfile.json and -DlockfileName="different-lockfile-name.json" is
// provided, validate should succeed
assertThat(result).isSuccessful();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1</version>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>fr.inria.gforge.spoon</groupId>
<artifactId>spoon-core</artifactId>
<version>10.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.github.chains-project</groupId>
<artifactId>
maven-lockfile</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>
generate</goal>
</goals>
</execution>
</executions>
<configuration>
<lockfileName>different-lockfile-name.json</lockfileName>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package simpleProject.src.main.java;

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Loading
Loading