Skip to content

Commit 03b1003

Browse files
authored
✨ feat: Add exactVersionString to freeze target (#1204)
1 parent ce475db commit 03b1003

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ mvn -f pom.lockfile.xml
7575
- `lockfileName` (`-DlockfileName=my-lockfile.json` default="lockfile.json") will set the name of the lockfile file to be generated/read.
7676
- `getConfigFromFile` will read the configuration of maven lockfile from the existing lockfile.
7777

78+
For `:freeze` target:
79+
- `exactVersionStrings` (`-DexactVersionStrings=false`, default=true) provide version string as exact parameter `[1.0.0]`, instead of soft requirement `1.0.0`.
80+
7881
### Flags example
7982

8083
The flags are passed by the maven [`-D` (`--define`)](https://books.sonatype.com/mvnref-book/reference/running-sect-options.html) property. For example, to set the `lockfileName` to `my-lockfile.json` and include maven plugins in the lockfile, you would run the following command:

maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/FreezeDependencyMojo.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class FreezeDependencyMojo extends AbstractMojo {
4848
@Parameter(defaultValue = "lockfile.json", property = "lockfileName")
4949
private String lockfileName;
5050

51+
@Parameter(defaultValue = "true", property = "exactVersionStrings")
52+
private String exactVersionStrings;
53+
5154
/**
5255
* Freezes the dependencies of the project. Every dependency will be locked to a specific version.
5356
*
@@ -143,11 +146,26 @@ private Dependency toMavenDependency(DependencyNode dep) {
143146
Dependency mavenDep = new Dependency();
144147
mavenDep.setGroupId(dep.getGroupId().getValue());
145148
mavenDep.setArtifactId(dep.getArtifactId().getValue());
146-
mavenDep.setVersion(dep.getVersion().getValue());
149+
String version = dep.getVersion().getValue();
150+
if (exactVersionStrings.equals("true")) {
151+
version = convertSoftToExactVersionString(version);
152+
}
153+
mavenDep.setVersion(version);
147154
if (dep.getClassifier() != null) {
148155
mavenDep.setClassifier(dep.getClassifier().getValue());
149156
}
150157
mavenDep.setScope(dep.getScope().getValue());
151158
return mavenDep;
152159
}
160+
161+
/**
162+
* Transform a soft version requirement into an exact one by wrapping it in a range which only includes .
163+
*/
164+
private String convertSoftToExactVersionString(String version) {
165+
if (version.startsWith("[") || version.startsWith("(")) {
166+
getLog().warn("Version is already a range, '" + version + "'. Cannot reliably make exact for freeze pom.");
167+
return version;
168+
}
169+
return "[" + version + "]";
170+
}
153171
}

maven_plugin/src/test/resources-its/it/IntegrationTestsIT/freezeWithDepManagement/pom.lockfile.expected.xml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.mycompany.app</groupId>
66
<artifactId>freeze-with-dep-management</artifactId>
@@ -15,42 +15,42 @@
1515
<dependency>
1616
<groupId>org.eclipse.sisu</groupId>
1717
<artifactId>org.eclipse.sisu.inject</artifactId>
18-
<version>0.9.0.M1</version>
18+
<version>[0.9.0.M1]</version>
1919
</dependency>
2020
<dependency>
2121
<groupId>javax.enterprise</groupId>
2222
<artifactId>cdi-api</artifactId>
23-
<version>1.2</version>
23+
<version>[1.2]</version>
2424
<scope>compile</scope>
2525
</dependency>
2626
<dependency>
2727
<groupId>org.codehaus.plexus</groupId>
2828
<artifactId>plexus-classworlds</artifactId>
29-
<version>2.6.0</version>
29+
<version>[2.6.0]</version>
3030
<scope>compile</scope>
3131
</dependency>
3232
<dependency>
3333
<groupId>javax.inject</groupId>
3434
<artifactId>javax.inject</artifactId>
35-
<version>1</version>
35+
<version>[1]</version>
3636
<scope>compile</scope>
3737
</dependency>
3838
<dependency>
3939
<groupId>org.codehaus.plexus</groupId>
4040
<artifactId>plexus-component-annotations</artifactId>
41-
<version>2.1.0</version>
41+
<version>[2.1.0]</version>
4242
<scope>compile</scope>
4343
</dependency>
4444
<dependency>
4545
<groupId>org.codehaus.plexus</groupId>
4646
<artifactId>plexus-utils</artifactId>
47-
<version>3.3.0</version>
47+
<version>[3.3.0]</version>
4848
<scope>compile</scope>
4949
</dependency>
5050
<dependency>
5151
<groupId>javax.annotation</groupId>
5252
<artifactId>javax.annotation-api</artifactId>
53-
<version>1.2</version>
53+
<version>[1.2]</version>
5454
<scope>compile</scope>
5555
</dependency>
5656
</dependencies>
@@ -59,16 +59,15 @@
5959
<dependency>
6060
<groupId>org.eclipse.sisu</groupId>
6161
<artifactId>org.eclipse.sisu.plexus</artifactId>
62-
<version>0.9.0.M2</version>
62+
<version>[0.9.0.M2]</version>
6363
<scope>compile</scope>
6464
</dependency>
6565
</dependencies>
6666
<build>
6767
<plugins>
6868
<plugin>
6969
<groupId>io.github.chains-project</groupId>
70-
<artifactId>
71-
maven-lockfile</artifactId>
70+
<artifactId>maven-lockfile</artifactId>
7271
<version>@project.version@</version>
7372
<executions>
7473
<execution>

maven_plugin/src/test/resources-its/it/IntegrationTestsIT/freezeWithoutDepManagement/pom.original.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
</goals>
6464
</execution>
6565
</executions>
66+
<configuration>
67+
<exactVersionStrings>false</exactVersionStrings>
68+
</configuration>
6669
</plugin>
6770
</plugins>
6871
</build>

maven_plugin/src/test/resources-its/it/IntegrationTestsIT/freezeWithoutDepManagement/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
</goals>
6464
</execution>
6565
</executions>
66+
<configuration>
67+
<exactVersionStrings>false</exactVersionStrings>
68+
</configuration>
6669
</plugin>
6770
</plugins>
6871
</build>

0 commit comments

Comments
 (0)