Skip to content

Commit

Permalink
Add the --ignore-inherited option to ignore build file artifacts with…
Browse files Browse the repository at this point in the history
… an inherited version
  • Loading branch information
AlexisJehan committed Feb 9, 2024
1 parent c57572f commit 57ab156
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features
- Display artifacts with an inherited version in parentheses
- Add the `--ignore-inherited` option to ignore build file artifacts with an inherited version

### Notes
- Update the `maven-core` dependency to `3.9.6`
Expand Down
93 changes: 67 additions & 26 deletions src/main/java/com/github/alexisjehan/mvncheck/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,57 +57,63 @@
public final class Application {

/**
* <p>Title.</p>
* @since 1.4.0
* <p>Maximum depth option long name.</p>
* @since 1.1.0
*/
private static final String TITLE = Constants.NAME + " " + Constants.VERSION
+ " (built with Maven " + MavenUtils.VERSION + " and Gradle " + GradleUtils.VERSION + ")";
static final String OPTION_MAX_DEPTH = "max-depth";

/**
* <p>Description.</p>
* <p>Help option long name.</p>
* @since 1.0.0
*/
private static final String DESCRIPTION = "Check for artifact updates of every "
+ ToString.toString(BuildFileType.MAVEN.getFileName()) + ", "
+ ToString.toString(BuildFileType.GRADLE_GROOVY.getFileName()) + " and "
+ ToString.toString(BuildFileType.GRADLE_KOTLIN.getFileName()) + " build files in the given or current "
+ "path recursively.";
static final String OPTION_HELP = "help";

/**
* <p>Command name.</p>
* @since 1.0.0
* <p>Ignore inherited option long name.</p>
* @since 1.5.0
*/
private static final String COMMAND_NAME = "mvnchk";
static final String OPTION_IGNORE_INHERITED = "ignore-inherited";

/**
* <p>Maximum depth option long name.</p>
* @since 1.1.0
* <p>Ignore snapshots option long name.</p>
* @since 1.0.0
*/
private static final String OPTION_MAX_DEPTH = "max-depth";
static final String OPTION_IGNORE_SNAPSHOTS = "ignore-snapshots";

/**
* <p>Help option long name.</p>
* <p>Short option long name.</p>
* @since 1.0.0
*/
private static final String OPTION_HELP = "help";
static final String OPTION_SHORT = "short";

/**
* <p>Ignore snapshots option long name.</p>
* <p>Version option long name.</p>
* @since 1.0.0
*/
private static final String OPTION_IGNORE_SNAPSHOTS = "ignore-snapshots";
static final String OPTION_VERSION = "version";

/**
* <p>Short option long name.</p>
* <p>Title.</p>
* @since 1.4.0
*/
private static final String TITLE = Constants.NAME + " " + Constants.VERSION
+ " (built with Maven " + MavenUtils.VERSION + " and Gradle " + GradleUtils.VERSION + ")";

/**
* <p>Description.</p>
* @since 1.0.0
*/
private static final String OPTION_SHORT = "short";
private static final String DESCRIPTION = "Check for artifact updates of every "
+ ToString.toString(BuildFileType.MAVEN.getFileName()) + ", "
+ ToString.toString(BuildFileType.GRADLE_GROOVY.getFileName()) + " and "
+ ToString.toString(BuildFileType.GRADLE_KOTLIN.getFileName()) + " build files in the given or current "
+ "path recursively.";

/**
* <p>Version option long name.</p>
* <p>Command name.</p>
* @since 1.0.0
*/
private static final String OPTION_VERSION = "version";
private static final String COMMAND_NAME = "mvnchk";

/**
* <p>Default value depending on whether ANSI should be enabled or not.</p>
Expand Down Expand Up @@ -146,6 +152,12 @@ public final class Application {
false,
"Display help information"
);
options.addOption(
null,
OPTION_IGNORE_INHERITED,
false,
"Ignore build file artifacts with an inherited version"
);
options.addOption(
"i",
OPTION_IGNORE_SNAPSHOTS,
Expand Down Expand Up @@ -245,6 +257,7 @@ void run(final String... args) {
commandLine.hasOption(OPTION_MAX_DEPTH)
? Integer.parseUnsignedInt(commandLine.getOptionValue(OPTION_MAX_DEPTH))
: DEFAULT_MAX_DEPTH,
commandLine.hasOption(OPTION_IGNORE_INHERITED),
commandLine.hasOption(OPTION_IGNORE_SNAPSHOTS),
commandLine.hasOption(OPTION_SHORT)
);
Expand All @@ -265,7 +278,11 @@ void run(final String... args) {
* @since 1.0.0
*/
@Deprecated(since = "1.1.0")
void run(final Path path, final boolean ignoreSnapshots, final boolean short0) throws IOException {
void run(
final Path path,
final boolean ignoreSnapshots,
final boolean short0
) throws IOException {
run(path, DEFAULT_MAX_DEPTH, ignoreSnapshots, short0);
}

Expand All @@ -278,11 +295,35 @@ void run(final Path path, final boolean ignoreSnapshots, final boolean short0) t
* @throws IOException might occur with input/output operations
* @throws NullPointerException if the path is {@code null}
* @throws IllegalArgumentException if the maximum depth is lower than {@code 0}
* @deprecated since 1.5.0, use {@link #run(Path, int, boolean, boolean, boolean)} instead
* @since 1.1.0
*/
@Deprecated(since = "1.5.0")
void run(
final Path path,
final int maxDepth,
final boolean ignoreSnapshots,
final boolean short0
) throws IOException {
run(path, maxDepth, false, ignoreSnapshots, short0);
}

/**
* <p>Run the program.</p>
* @param path a path
* @param maxDepth a maximum depth
* @param ignoreInherited {@code true} if build file artifacts with an inherited version should be ignored
* @param ignoreSnapshots {@code true} if build file artifacts with a snapshot version should be ignored
* @param short0 {@code true} if only build files with at least one artifact update should be shown
* @throws IOException might occur with input/output operations
* @throws NullPointerException if the path is {@code null}
* @throws IllegalArgumentException if the maximum depth is lower than {@code 0}
* @since 1.5.0
*/
void run(
final Path path,
final int maxDepth,
final boolean ignoreInherited,
final boolean ignoreSnapshots,
final boolean short0
) throws IOException {
Expand All @@ -303,7 +344,7 @@ void run(
final List<ArtifactUpdateVersion> artifactUpdateVersions;
try {
final var build = service.findBuild(buildFile);
artifactUpdateVersions = service.findArtifactUpdateVersions(build, ignoreSnapshots);
artifactUpdateVersions = service.findArtifactUpdateVersions(build, ignoreInherited, ignoreSnapshots);
} catch (final BuildResolveException | ArtifactAvailableVersionsResolveException e) {
outputStream.println(Ansi.ansi().fgBrightRed().a(toString(file)).reset());
outputStream.println(Ansi.ansi().fgBrightRed().a(toString(e)).reset());
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/github/alexisjehan/mvncheck/core/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,31 @@ public Build findBuild(final BuildFile buildFile) {
* @return the {@link List} of artifact update versions
* @throws IOException might occur with input/output operations
* @throws NullPointerException if the build is {@code null}
* @deprecated since 1.5.0, use {@link #findArtifactUpdateVersions(Build, boolean, boolean)} instead
* @since 1.0.0
*/
@Deprecated(since = "1.5.0")
public List<ArtifactUpdateVersion> findArtifactUpdateVersions(
final Build build,
final boolean ignoreSnapshots
) throws IOException {
return findArtifactUpdateVersions(build, false, ignoreSnapshots);
}

/**
* <p>Find a {@link List} of artifact update versions for the given build.</p>
* @param build a build
* @param ignoreInherited {@code true} if build file artifacts with an inherited version should be ignored
* @param ignoreSnapshots {@code true} if build file artifacts with a snapshot version should be ignored
* @return the {@link List} of artifact update versions
* @throws IOException might occur with input/output operations
* @throws NullPointerException if the build is {@code null}
* @since 1.5.0
*/
public List<ArtifactUpdateVersion> findArtifactUpdateVersions(
final Build build,
final boolean ignoreInherited,
final boolean ignoreSnapshots
) throws IOException {
Ensure.notNull("build", build);
final var artifactFilter = new CompositeArtifactFilter(
Expand All @@ -220,6 +240,7 @@ public List<ArtifactUpdateVersion> findArtifactUpdateVersions(
return build.getArtifacts()
.parallelStream()
.filter(artifactFilter::accept)
.filter(artifact -> !ignoreInherited || !artifact.isVersionInherited())
.filter(
artifact -> artifact.getOptionalVersion()
.filter(version -> !ignoreSnapshots || !VersionFilter.SNAPSHOT.accept(version))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void testRun(@TempDir final Path tmpDirectory) throws IOException {
try (var outputStream = new ByteArrayOutputStream()) {
try (var printStream = new PrintStream(outputStream)) {
final var application = new Application(printStream);
application.run(tmpDirectory, 1, false, false);
application.run(tmpDirectory, 1, false, false, false);
}
assertThat(outputStream.toString()).matches(
"^"
Expand Down
Loading

0 comments on commit 57ab156

Please sign in to comment.