Skip to content

Commit 4d541b0

Browse files
authored
Merge pull request #1 from arrudalabs/new-version
First implementation
2 parents 5521a2f + 49590d3 commit 4d541b0

23 files changed

Lines changed: 1215 additions & 142 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,6 @@ dist/
324324
nbdist/
325325
.nb-gradle/
326326

327-
# End of https://www.toptal.com/developers/gitignore/api/maven,intellij,intellij+iml,intellij+all,netbeans,eclipse
327+
# End of https://www.toptal.com/developers/gitignore/api/maven,intellij,intellij+iml,intellij+all,netbeans,eclipse.teste/*
328+
329+
.teste/

pom.xml

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
~ limitations under the License.
1616
~
1717
-->
18-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1920
<modelVersion>4.0.0</modelVersion>
2021

2122
<groupId>io.github.arrudalabs</groupId>
@@ -315,90 +316,93 @@
315316
<module name="Checker">
316317

317318
<module name="FileLength">
318-
<property name="max" value="3500" />
319-
<property name="fileExtensions" value="java" />
319+
<property name="max" value="3500"/>
320+
<property name="fileExtensions" value="java"/>
320321
</module>
321322

322323
<module name="LineLength">
323-
<property name="max" value="180" />
324-
<property name="ignorePattern" value="@version|@see" />
324+
<property name="max" value="180"/>
325+
<property name="ignorePattern" value="@version|@see"/>
325326
</module>
326327

327-
<module name="FileTabCharacter" />
328+
<module name="FileTabCharacter"/>
328329

329330
<module name="TreeWalker">
330-
<module name="SuppressionCommentFilter" />
331+
<module name="SuppressionCommentFilter"/>
331332
<module name="ConstantName">
332-
<property name="format" value="^(_?[a-z][a-zA-Z0-9]*|([A-Z](_?[A-Z0-9]+)*))$" />
333+
<property name="format" value="^(_?[a-z][a-zA-Z0-9]*|([A-Z](_?[A-Z0-9]+)*))$"/>
333334
</module>
334335

335-
<module name="LocalVariableName" />
336+
<module name="LocalVariableName"/>
336337
<module name="MethodName">
337-
<property name="format" value="^_?[a-z][a-zA-Z0-9]*$" />
338+
<property name="format" value="^_?[a-z][a-zA-Z0-9]*$"/>
338339
</module>
339-
<module name="PackageName" />
340-
<module name="LocalFinalVariableName" />
341-
<module name="ParameterName" />
342-
<module name="StaticVariableName" />
340+
<module name="PackageName"/>
341+
<module name="LocalFinalVariableName"/>
342+
<module name="ParameterName"/>
343+
<module name="StaticVariableName"/>
343344
<module name="TypeName">
344-
<property name="format" value="^_?[A-Z][a-zA-Z0-9]*$" />
345+
<property name="format" value="^_?[A-Z][a-zA-Z0-9]*$"/>
345346
</module>
346347

347348
<module name="AvoidStarImport">
348-
<property name="excludes" value="java.io,java.net,java.util,jakarta.enterprise.inject.spi,jakarta.enterprise.context" />
349+
<property name="excludes"
350+
value="java.io,java.net,java.util,jakarta.enterprise.inject.spi,jakarta.enterprise.context"/>
349351
</module>
350-
<module name="IllegalImport" />
351-
<module name="RedundantImport" />
352-
<module name="UnusedImports" />
352+
<module name="IllegalImport"/>
353+
<module name="RedundantImport"/>
354+
<module name="UnusedImports"/>
353355

354356
<module name="MethodLength">
355-
<property name="max" value="250" />
357+
<property name="max" value="250"/>
356358
</module>
357359
<module name="ParameterNumber">
358-
<property name="max" value="15" />
360+
<property name="max" value="15"/>
359361
</module>
360362

361363
<module name="EmptyBlock">
362-
<property name="option" value="text" />
364+
<property name="option" value="text"/>
363365
</module>
364366

365367
<module name="NeedBraces">
366-
<property name="tokens" value="LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_WHILE" />
368+
<property name="tokens"
369+
value="LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_WHILE"/>
367370
</module>
368371
<module name="LeftCurly">
369-
<property name="option" value="EOL" />
372+
<property name="option" value="EOL"/>
370373
</module>
371374
<module name="RightCurly">
372-
<property name="option" value="SAME" />
375+
<property name="option" value="SAME"/>
373376
</module>
374377

375-
<module name="EmptyStatement" />
376-
<module name="EqualsHashCode" />
377-
<module name="DefaultComesLast" />
378-
<module name="MissingSwitchDefault" />
379-
<module name="FallThrough" />
380-
<module name="MultipleVariableDeclarations" />
378+
<module name="EmptyStatement"/>
379+
<module name="EqualsHashCode"/>
380+
<module name="DefaultComesLast"/>
381+
<module name="MissingSwitchDefault"/>
382+
<module name="FallThrough"/>
383+
<module name="MultipleVariableDeclarations"/>
381384

382385
<module name="com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck">
383-
<property name="severity" value="ignore" />
386+
<property name="severity" value="ignore"/>
384387
</module>
385388

386-
<module name="HideUtilityClassConstructor" />
389+
<module name="HideUtilityClassConstructor"/>
387390

388391
<module name="com.puppycrawl.tools.checkstyle.checks.design.VisibilityModifierCheck">
389-
<property name="packageAllowed" value="true" />
390-
<property name="protectedAllowed" value="true" />
391-
<property name="publicMemberPattern" value="^serialVersionUID" />
392-
<property name="severity" value="warning" />
392+
<property name="packageAllowed" value="true"/>
393+
<property name="protectedAllowed" value="true"/>
394+
<property name="publicMemberPattern" value="^serialVersionUID"/>
395+
<property name="severity" value="warning"/>
393396
</module>
394397

395-
<module name="UpperEll" />
398+
<module name="UpperEll"/>
396399

397400
<module name="AnnotationLocation">
398-
<property name="allowSamelineMultipleAnnotations" value="false" />
399-
<property name="allowSamelineSingleParameterlessAnnotation" value="false" />
400-
<property name="allowSamelineParameterizedAnnotation" value="false" />
401-
<property name="tokens" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF, ANNOTATION_DEF" />
401+
<property name="allowSamelineMultipleAnnotations" value="false"/>
402+
<property name="allowSamelineSingleParameterlessAnnotation" value="false"/>
403+
<property name="allowSamelineParameterizedAnnotation" value="false"/>
404+
<property name="tokens"
405+
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF, ANNOTATION_DEF"/>
402406
</module>
403407
</module>
404408
</module>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.arrudalabs.pomeditor;
18+
19+
import java.nio.file.Path;
20+
import java.util.Objects;
21+
22+
/**
23+
* It's parameters for an add dependency request
24+
*/
25+
class AddDependency {
26+
27+
final Path pom;
28+
final Dependency dependency;
29+
30+
/**
31+
* Creates an AddDependencyRequest instance
32+
*
33+
* @param pom path to the target POM. By default,: pom.xml
34+
* @param dependency a {@link Dependency} instance
35+
* @throws IllegalArgumentException when pom or dependency is null
36+
* @throws IllegalArgumentException when pom or dependency is null
37+
*/
38+
AddDependency(Path pom, Dependency dependency) {
39+
Objects.requireNonNull(pom, "pom path is required");
40+
Objects.requireNonNull(dependency, "dependency is required");
41+
this.pom = pom;
42+
this.dependency = dependency;
43+
}
44+
45+
}

src/main/java/io/github/arrudalabs/pomeditor/AddDependencyMojo.java

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,91 +19,76 @@
1919
import org.apache.maven.plugin.AbstractMojo;
2020
import org.apache.maven.plugin.MojoExecutionException;
2121
import org.apache.maven.plugin.MojoFailureException;
22+
import org.apache.maven.plugin.logging.Log;
2223
import org.apache.maven.plugins.annotations.Mojo;
2324
import org.apache.maven.plugins.annotations.Parameter;
24-
import org.l2x6.pom.tuner.PomTransformer;
25-
import org.l2x6.pom.tuner.PomTransformer.Transformation;
26-
import org.l2x6.pom.tuner.model.Gavtcs;
2725

28-
import java.nio.charset.StandardCharsets;
2926
import java.nio.file.Path;
3027
import java.nio.file.Paths;
28+
import java.util.Optional;
3129
import java.util.function.BiConsumer;
3230

3331
/**
34-
* add a given dependency to a target POM
32+
* Mojo responsible to add a given dependency to a target POM
33+
* if such dependency is not declared
34+
* or the given dependency's version is greater than the existent at target POM
3535
*/
3636
@Mojo(name = "add-dep")
3737
public class AddDependencyMojo extends AbstractMojo {
3838

39-
private static BiConsumer<Path, AddDependencyMojo> defaultPomTransformer = (pomPath, params) -> {
40-
new PomTransformer(pomPath, StandardCharsets.UTF_8,
41-
PomTransformer.SimpleElementWhitespace.AUTODETECT_PREFER_EMPTY)
42-
.transform(
43-
Transformation.addDependencyIfNeeded(
44-
new Gavtcs(params.groupId,
45-
params.artifactId,
46-
params.version,
47-
params.type,
48-
params.classifier,
49-
params.scope),
50-
Gavtcs.scopeAndTypeFirstComparator())
51-
);
52-
};
53-
5439
@Parameter(property = "groupId")
5540
String groupId;
5641
@Parameter(property = "artifactId")
5742
String artifactId;
58-
@Parameter(property = "version", defaultValue = "")
59-
String version = "";
60-
@Parameter(property = "type", defaultValue = "")
61-
String type = "";
62-
@Parameter(property = "classifier", defaultValue = "")
63-
String classifier = "";
64-
@Parameter(property = "scope", defaultValue = "")
65-
String scope = "";
43+
@Parameter(property = "version")
44+
String version;
45+
@Parameter(property = "type")
46+
String type;
47+
@Parameter(property = "classifier")
48+
String classifier;
49+
@Parameter(property = "scope")
50+
String scope;
6651
@Parameter(property = "pom", defaultValue = "pom.xml")
6752
String pom = "pom.xml";
6853

69-
BiConsumer<Path, AddDependencyMojo> pomTransformer;
54+
PomEditor pomEditor;
7055

71-
BiConsumer<Path, AddDependencyMojo> getPomTransformer() {
72-
if (this.pomTransformer == null) {
73-
return defaultPomTransformer;
74-
}
75-
return this.pomTransformer;
76-
}
56+
BiConsumer<Log, Path> backupFunction;
57+
58+
BiConsumer<Log, Path> rollbackFunction;
7759

78-
void validate() throws MojoExecutionException {
79-
validateGroupId();
80-
validateArtifactIt();
81-
}
8260

8361
@Override
8462
public void execute() throws MojoExecutionException, MojoFailureException {
85-
validate();
63+
Path pomFile = Paths.get(pom);
64+
boolean doRollback = false;
8665
try {
87-
getPomTransformer().accept(Paths.get(pom), this);
66+
Optional.ofNullable(backupFunction)
67+
.orElse(PomChangeTransaction::backup)
68+
.accept(getLog(), pomFile);
69+
70+
Optional.ofNullable(pomEditor)
71+
.orElseGet(PomEditor::new)
72+
.execute(
73+
new AddDependency(
74+
pomFile,
75+
Dependency.builder()
76+
.setGroupId(groupId)
77+
.setArtifactId(artifactId)
78+
.setVersion(version)
79+
.setType(type)
80+
.setClassifier(classifier)
81+
.setScope(scope)
82+
.build()));
8883
} catch (RuntimeException ex) {
84+
doRollback = true;
8985
throw new MojoExecutionException(ex);
86+
} finally {
87+
if (doRollback) {
88+
Optional.ofNullable(rollbackFunction)
89+
.orElse(PomChangeTransaction::rollback)
90+
.accept(getLog(), pomFile);
91+
}
9092
}
9193
}
92-
93-
private boolean isNullOrBlank(String artifactId) {
94-
return artifactId == null || artifactId.isBlank();
95-
}
96-
97-
private void validateArtifactIt() throws MojoExecutionException {
98-
if (isNullOrBlank(artifactId)) {
99-
throw new MojoExecutionException("artifactId cannot be null or empty");
100-
}
101-
}
102-
103-
private void validateGroupId() throws MojoExecutionException {
104-
if (isNullOrBlank(groupId)) {
105-
throw new MojoExecutionException("groupId cannot be null or empty");
106-
}
107-
}
108-
10994
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.arrudalabs.pomeditor;
18+
19+
import org.apache.maven.plugin.AbstractMojo;
20+
import org.apache.maven.plugin.MojoExecutionException;
21+
import org.apache.maven.plugin.MojoFailureException;
22+
import org.apache.maven.plugin.logging.Log;
23+
import org.apache.maven.plugins.annotations.Mojo;
24+
import org.apache.maven.plugins.annotations.Parameter;
25+
26+
import java.nio.file.Path;
27+
import java.util.Optional;
28+
import java.util.function.BiConsumer;
29+
30+
/**
31+
* Responsible for delete the backup file
32+
*/
33+
@Mojo(name = "commit")
34+
public class CommitMojo extends AbstractMojo {
35+
36+
@Parameter(property = "pom", defaultValue = "pom.xml")
37+
String pom = "pom.xml";
38+
39+
BiConsumer<Log, Path> commitFunction;
40+
41+
@Override
42+
public void execute() throws MojoExecutionException, MojoFailureException {
43+
Optional.ofNullable(commitFunction)
44+
.orElse(PomChangeTransaction::commit)
45+
.accept(getLog(), Path.of(pom));
46+
}
47+
}

0 commit comments

Comments
 (0)