Skip to content

Commit

Permalink
Update common/maven-plugin (#1047)
Browse files Browse the repository at this point in the history
- Move MavenPattern / Filter from maven-plugins/javadoc-maven-plugin
- Rename Filter to MavenFilter
- Add MavenArtifact to adapter all artifacts / gav interfaces for filters and patterns
- Update MavenModel to follow the getter convention (not get prefix)
- Update plexus-component to 2.2.0 (to support records)
  • Loading branch information
romain-grecourt authored Jul 25, 2024
1 parent 8af2d5b commit 9a1f555
Show file tree
Hide file tree
Showing 20 changed files with 421 additions and 205 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -101,10 +101,10 @@ static void requireValidMavenProjectConfig(CommonOptions commonOptions) {
if (!Files.exists(projectConfigFile)) {
// Find the helidon version if we can and create the config file
MavenModel model = MavenModel.read(pomFile);
Parent parent = model.getParent();
Parent parent = model.parent();
String helidonVersion = null;
if (parent != null && parent.getGroupId().startsWith("io.helidon.")) {
helidonVersion = parent.getVersion();
if (parent != null && parent.groupId().startsWith("io.helidon.")) {
helidonVersion = parent.version();
}
ensureProjectConfig(projectDir, helidonVersion);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -520,21 +520,21 @@ public CommandInvoker assertExpectedPom() {
MavenModel model = MavenModel.read(pomFile);

// Flavor
String parentArtifact = model.getParent().getArtifactId();
String parentArtifact = model.parent().artifactId();
assertThat(parentArtifact, containsString(flavor.toLowerCase()));

// GroupId
assertThat(model.getGroupId(), is(groupId));
assertThat(model.groupId(), is(groupId));

// ArtifactId
assertThat(model.getArtifactId(), is(artifactId));
assertThat(model.artifactId(), is(artifactId));

// Project Name
assertThat(model.getName(), is(projectName));
assertThat(model.name(), is(projectName));

if (helidonVersion != null) {
// Project Version
assertThat(model.getParent().getVersion(), is(helidonVersion));
assertThat(model.parent().version(), is(helidonVersion));
}

return this;
Expand Down
19 changes: 19 additions & 0 deletions common/maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@
<artifactId>helidon-build-common-maven-plugin</artifactId>
<name>Helidon Build Tools Common Maven Plugin</name>

<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.build-tools.common</groupId>
<artifactId>helidon-build-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.build-tools.common</groupId>
<artifactId>helidon-build-common-maven</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.build-tools.common</groupId>
<artifactId>helidon-build-common-ansi</artifactId>
Expand All @@ -43,6 +52,16 @@
<artifactId>maven-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.build.common.maven.plugin;

import java.nio.file.Path;
import java.util.function.Supplier;

import io.helidon.build.common.maven.MavenModel;

/**
* Maven Artifact.
*
* @param groupId groupId
* @param artifactId artifactId
* @param version version
* @param classifier classifier
* @param type type
* @param file file
*/
public record MavenArtifact(String groupId, String artifactId, String version, String classifier, String type, Path file) {

/**
* Create a new instance.
*
* @param groupId groupId
* @param artifactId artifactId
* @param version version
* @param classifier classifier
* @param type type
*/
public MavenArtifact(String groupId, String artifactId, String version, String classifier, String type) {
this(groupId, artifactId, version, classifier, type, null);
}

/**
* Create a new instance.
*
* @param a artifact
*/
public MavenArtifact(org.eclipse.aether.artifact.Artifact a) {
this(a.getGroupId(), a.getArtifactId(), a.getVersion(), a.getClassifier(), a.getExtension(),
a.getFile() != null ? a.getFile().toPath() : null);
}

/**
* Create a new instance.
*
* @param a artifact
*/
public MavenArtifact(org.apache.maven.artifact.Artifact a) {
this(a.getGroupId(), a.getArtifactId(), a.getVersion(), a.getClassifier(), a.getType(),
a.getFile() != null ? a.getFile().toPath() : null);
}

/**
* Create a new instance.
*
* @param d dependency
*/
public MavenArtifact(org.apache.maven.model.Dependency d) {
this(d.getGroupId(), d.getArtifactId(), d.getVersion(), d.getClassifier(), d.getType());
}

/**
* Create a new instance.
*
* @param d dependency
* @param supplier version supplier
*/
public MavenArtifact(org.apache.maven.model.Dependency d, Supplier<String> supplier) {
this(d.getGroupId(), d.getArtifactId(), d.getVersion() == null ? supplier.get() : d.getVersion(),
d.getClassifier(), d.getType());
}

/**
* Create a new instance.
*
* @param p plugin
*/
public MavenArtifact(org.apache.maven.model.Plugin p) {
this(p.getGroupId(), p.getArtifactId(), p.getVersion(), null, "jar");
}

/**
* Create a new instance.
*
* @param p plugin
* @param supplier supplier resolver
*/
public MavenArtifact(org.apache.maven.model.Plugin p, Supplier<String> supplier) {
this(p.getGroupId(), p.getArtifactId(), p.getVersion() == null ? supplier.get() : p.getVersion(), null, "jar");
}

/**
* Create a new instance.
*
* @param m model
*/
public MavenArtifact(MavenModel m) {
this(m.groupId(), m.artifactId(), m.version(), null, "jar");
}

/**
* Convert this instance to {@link org.eclipse.aether.artifact.Artifact}.
*
* @return artifact
*/
public org.eclipse.aether.artifact.Artifact toAetherArtifact() {
return new org.eclipse.aether.artifact.DefaultArtifact(groupId, artifactId, classifier, type, version);
}

/**
* Get the corresponding {@code .pom} artifact.
*
* @return artifact
*/
public MavenArtifact pom() {
return new MavenArtifact(groupId, artifactId, version, null, "pom");
}

/**
* Get the corresponding {@code -sources.jar} artifact.
*
* @return artifact
*/
public MavenArtifact sourcesJar() {
return new MavenArtifact(groupId, artifactId, version, "sources", "jar");
}

@Override
public String toString() {
return groupId
+ ":" + artifactId
+ ":" + version
+ ":" + (classifier == null ? "" : classifier)
+ ":" + type;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.build.javadoc;
package io.helidon.build.common.maven.plugin;

import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -26,15 +26,13 @@
import io.helidon.build.common.SourcePath;
import io.helidon.build.common.maven.MavenModel;

import org.apache.maven.artifact.Artifact;

/**
* Filters.
* Maven filters.
*/
final class Filters {
public final class MavenFilters {

private Filters() {
// cannot be instanciated
private MavenFilters() {
// cannot be instantiated
}

/**
Expand All @@ -43,7 +41,7 @@ private Filters() {
* @param paths paths that must exists when resolved against the tested path
* @return predicate
*/
static Predicate<Path> dirFilter(List<String> paths) {
public static Predicate<Path> dirFilter(List<String> paths) {
return dir -> Files.isDirectory(dir) && paths.stream().map(dir::resolve).allMatch(Files::exists);
}

Expand All @@ -55,7 +53,7 @@ static Predicate<Path> dirFilter(List<String> paths) {
* @param dir root directory used to relativize the paths
* @return predicate
*/
static Predicate<Path> pathFilter(List<String> includes, List<String> excludes, Path dir) {
public static Predicate<Path> pathFilter(List<String> includes, List<String> excludes, Path dir) {
return filter(includes, excludes, Function.identity(), p -> new SourcePath(dir, p), SourcePath::matches);
}

Expand All @@ -66,18 +64,18 @@ static Predicate<Path> pathFilter(List<String> includes, List<String> excludes,
* @param excludes exclude patterns with wildcard support
* @return predicate
*/
static Predicate<String> stringFilter(List<String> includes, List<String> excludes) {
public static Predicate<String> stringFilter(List<String> includes, List<String> excludes) {
return filter(includes, excludes, Function.identity(), Function.identity(), SourcePath::wildcardMatch);
}

/**
* Create a new predicate for {@link Artifact} that matches include and exclude patterns.
* Create a new predicate for {@link MavenArtifact} that matches include and exclude patterns.
*
* @param includes include patterns (see {@link MavenPattern}
* @param excludes exclude patterns (see {@link MavenPattern}
* @return predicate
*/
static Predicate<Artifact> artifactFilter(List<String> includes, List<String> excludes) {
public static Predicate<MavenArtifact> artifactFilter(List<String> includes, List<String> excludes) {
return filter(includes, excludes, MavenPattern::create, Function.identity(), (a, p) -> p.matches(a));
}

Expand All @@ -88,7 +86,7 @@ static Predicate<Artifact> artifactFilter(List<String> includes, List<String> ex
* @param excludes exclude patterns (see {@link MavenPattern}
* @return predicate
*/
static Predicate<MavenModel> pomFilter(List<String> includes, List<String> excludes) {
public static Predicate<MavenModel> pomFilter(List<String> includes, List<String> excludes) {
return filter(includes, excludes, MavenPattern::create, Function.identity(), (a, p) -> p.matches(a));
}

Expand All @@ -105,11 +103,11 @@ static Predicate<MavenModel> pomFilter(List<String> includes, List<String> exclu
* @param <V> mapped input type
* @return predicate
*/
static <T, U, V> Predicate<U> filter(List<String> includes,
List<String> excludes,
Function<String, T> patternFactory,
Function<U, V> mapper,
BiFunction<V, T, Boolean> predicate) {
public static <T, U, V> Predicate<U> filter(List<String> includes,
List<String> excludes,
Function<String, T> patternFactory,
Function<U, V> mapper,
BiFunction<V, T, Boolean> predicate) {

List<T> includePatterns = Lists.map(includes, patternFactory);
List<T> excludePatterns = Lists.map(excludes, patternFactory);
Expand All @@ -119,5 +117,4 @@ static <T, U, V> Predicate<U> filter(List<String> includes,
&& excludePatterns.stream().noneMatch(it -> predicate.apply(v, it));
};
}

}
Loading

0 comments on commit 9a1f555

Please sign in to comment.