This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git
commit 8f58d9bf9fc3f32862c4d96f0789f6d7f5e5a702 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 00:55:27 2026 +0100 Add requireExplicitDependencies to copy and unpack --- .../child-a/pom.xml | 39 +++++ .../child-a/src/main/resources/DoNotDelete.xml | 20 +++ .../child-b/pom.xml | 77 +++++++++ .../invoker.properties | 18 +++ .../require-explicit-dependencies-declared/pom.xml | 53 +++++++ .../verify.groovy | 23 +++ .../child-a/pom.xml | 39 +++++ .../child-a/src/main/resources/DoNotDelete.xml | 20 +++ .../child-b/pom.xml | 69 ++++++++ .../invoker.properties | 19 +++ .../pom.xml | 53 +++++++ .../verify.groovy | 25 +++ .../AbstractFromConfigurationMojo.java | 42 +++++ .../ExplicitDependencyMatcher.java | 107 +++++++++++++ src/site/markdown/usage.md.vm | 4 + .../ExplicitDependencyMatcherTest.java | 175 +++++++++++++++++++++ 16 files changed, 783 insertions(+) diff --git a/src/it/projects/require-explicit-dependencies-declared/child-a/pom.xml b/src/it/projects/require-explicit-dependencies-declared/child-a/pom.xml new file mode 100644 index 00000000..1f05edf1 --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-declared/child-a/pom.xml @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you 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. + --> + +<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"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>test</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + <artifactId>child-a</artifactId> + <packaging>jar</packaging> + + <name>Copy from reactor source of dependency</name> + <description> + Test dependency:copy when searching the reactor + </description> + +</project> diff --git a/src/it/projects/require-explicit-dependencies-declared/child-a/src/main/resources/DoNotDelete.xml b/src/it/projects/require-explicit-dependencies-declared/child-a/src/main/resources/DoNotDelete.xml new file mode 100644 index 00000000..4d3d9e49 --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-declared/child-a/src/main/resources/DoNotDelete.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you 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. + --> +<root/> diff --git a/src/it/projects/require-explicit-dependencies-declared/child-b/pom.xml b/src/it/projects/require-explicit-dependencies-declared/child-b/pom.xml new file mode 100644 index 00000000..fa2ce9cb --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-declared/child-b/pom.xml @@ -0,0 +1,77 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you 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. + --> + +<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"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>test</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + <artifactId>child-b</artifactId> + <packaging>pom</packaging> + + <name>Require explicit dependencies (declared)</name> + <description> + Test dependency:unpack with requireExplicitDependencies + </description> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>child-a</artifactId> + <version>1.0-SNAPSHOT</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <artifactId>maven-dependency-plugin</artifactId> + <version>@project.version@</version> + <executions> + <execution> + <id>test</id> + <goals> + <goal>unpack</goal> + </goals> + <configuration> + <requireExplicitDependencies>true</requireExplicitDependencies> + <artifactItems> + <artifactItem> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>child-a</artifactId> + <version>1.0-SNAPSHOT</version> + </artifactItem> + </artifactItems> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/projects/require-explicit-dependencies-declared/invoker.properties b/src/it/projects/require-explicit-dependencies-declared/invoker.properties new file mode 100644 index 00000000..2e199f7b --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-declared/invoker.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +invoker.goals = clean package diff --git a/src/it/projects/require-explicit-dependencies-declared/pom.xml b/src/it/projects/require-explicit-dependencies-declared/pom.xml new file mode 100644 index 00000000..5ee2ceeb --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-declared/pom.xml @@ -0,0 +1,53 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you 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. + --> + +<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"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>test</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>pom</packaging> + + <modules> + <module>child-a</module> + <module>child-b</module> + </modules> + + <name>Require explicit dependencies (declared)</name> + <description> + Test dependency:unpack with requireExplicitDependencies + </description> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <build> + <plugins> + <plugin> + <artifactId>maven-dependency-plugin</artifactId> + <version>@project.version@</version> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/projects/require-explicit-dependencies-declared/verify.groovy b/src/it/projects/require-explicit-dependencies-declared/verify.groovy new file mode 100644 index 00000000..5a1ad70d --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-declared/verify.groovy @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +File unpacked = new File( basedir, "child-b/target/dependency/DoNotDelete.xml" ) +assert unpacked.isFile() : "Missing file " + unpacked + +return true; diff --git a/src/it/projects/require-explicit-dependencies-undeclared/child-a/pom.xml b/src/it/projects/require-explicit-dependencies-undeclared/child-a/pom.xml new file mode 100644 index 00000000..1f05edf1 --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-undeclared/child-a/pom.xml @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you 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. + --> + +<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"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>test</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + <artifactId>child-a</artifactId> + <packaging>jar</packaging> + + <name>Copy from reactor source of dependency</name> + <description> + Test dependency:copy when searching the reactor + </description> + +</project> diff --git a/src/it/projects/require-explicit-dependencies-undeclared/child-a/src/main/resources/DoNotDelete.xml b/src/it/projects/require-explicit-dependencies-undeclared/child-a/src/main/resources/DoNotDelete.xml new file mode 100644 index 00000000..4d3d9e49 --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-undeclared/child-a/src/main/resources/DoNotDelete.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you 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. + --> +<root/> diff --git a/src/it/projects/require-explicit-dependencies-undeclared/child-b/pom.xml b/src/it/projects/require-explicit-dependencies-undeclared/child-b/pom.xml new file mode 100644 index 00000000..a020f89d --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-undeclared/child-b/pom.xml @@ -0,0 +1,69 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you 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. + --> + +<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"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>test</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + <artifactId>child-b</artifactId> + <packaging>pom</packaging> + + <name>Require explicit dependencies (undeclared)</name> + <description> + Test dependency:unpack with requireExplicitDependencies + </description> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <build> + <plugins> + <plugin> + <artifactId>maven-dependency-plugin</artifactId> + <version>@project.version@</version> + <executions> + <execution> + <id>test</id> + <goals> + <goal>unpack</goal> + </goals> + <configuration> + <requireExplicitDependencies>true</requireExplicitDependencies> + <artifactItems> + <artifactItem> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>child-a</artifactId> + <version>1.0-SNAPSHOT</version> + </artifactItem> + </artifactItems> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/projects/require-explicit-dependencies-undeclared/invoker.properties b/src/it/projects/require-explicit-dependencies-undeclared/invoker.properties new file mode 100644 index 00000000..415ac78e --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-undeclared/invoker.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +invoker.goals = clean package +invoker.buildResult = failure diff --git a/src/it/projects/require-explicit-dependencies-undeclared/pom.xml b/src/it/projects/require-explicit-dependencies-undeclared/pom.xml new file mode 100644 index 00000000..93f97f91 --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-undeclared/pom.xml @@ -0,0 +1,53 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you 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. + --> + +<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"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>test</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>pom</packaging> + + <modules> + <module>child-a</module> + <module>child-b</module> + </modules> + + <name>Require explicit dependencies (undeclared)</name> + <description> + Test dependency:unpack with requireExplicitDependencies + </description> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <build> + <plugins> + <plugin> + <artifactId>maven-dependency-plugin</artifactId> + <version>@project.version@</version> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/projects/require-explicit-dependencies-undeclared/verify.groovy b/src/it/projects/require-explicit-dependencies-undeclared/verify.groovy new file mode 100644 index 00000000..f7d2b51a --- /dev/null +++ b/src/it/projects/require-explicit-dependencies-undeclared/verify.groovy @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +String log = new File( basedir, "build.log" ).text +assert log.contains( "requireExplicitDependencies is set" ) : "Missing failure message" +assert log.contains( "org.apache.maven.its.dependency:child-a:1.0-SNAPSHOT:jar is not declared as a dependency" ) : "Missing offending item" +assert !new File( basedir, "child-b/target/dependency/DoNotDelete.xml" ).exists() + +return true; diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java index 818a7f15..2ea9dd1c 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java @@ -138,6 +138,22 @@ public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMo @Parameter(property = "remoteRepositories") private List<String> remoteRepositories; + /** + * Fail if an item of {@code artifactItems} is not declared as a dependency of the project, or is declared with a + * different version. Maven orders the reactor by the declared dependencies only, so an undeclared item that is + * built by another module of the reactor may be missing or stale. Declare such an item as a dependency, for + * example with scope {@code provided} or {@code test}, or with type {@code pom} where only its content is needed. + * Items match on groupId, artifactId, type and classifier, where {@code test-jar} equals {@code jar} with the + * classifier {@code tests}. The check only applies to items configured in the POM, not to the command line + * parameter {@code artifact}. + * + * @since 3.12.0 + */ + @Parameter(property = "mdep.requireExplicitDependencies", defaultValue = "false") + private boolean requireExplicitDependencies; + + private boolean artifactFromCommandLine; + private final ArtifactHandlerManager artifactHandlerManager; private final ResolverUtil resolverUtil; @@ -168,6 +184,22 @@ public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMo if (artifactItems == null || artifactItems.isEmpty()) { throw new MojoFailureException("Either artifact or artifactItems is required "); } + if (requireExplicitDependencies) { + if (artifactFromCommandLine) { + getLog().warn("requireExplicitDependencies is ignored for the artifact parameter"); + } else { + List<String> violations = new ExplicitDependencyMatcher( + artifactHandlerManager, getProject().getDependencies()) + .findViolations(artifactItems); + if (!violations.isEmpty()) { + throw new MojoFailureException("requireExplicitDependencies is set, but artifactItems must be " + + "declared as dependencies of the project, so that Maven orders the reactor by them:" + + System.lineSeparator() + " " + String.join(System.lineSeparator() + " ", violations) + + System.lineSeparator() + "Declare each of them in the dependencies of the project, " + + "for example with scope provided or test, or with type pom, and use the same version."); + } + } + } } /** @@ -338,6 +370,7 @@ public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMo */ public void setArtifactItems(List<ArtifactItem> theArtifactItems) { this.artifactItems = theArtifactItems; + this.artifactFromCommandLine = false; } /** @@ -396,6 +429,14 @@ public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMo this.overWriteSnapshots = theOverWriteSnapshots; } + /** + * @param requireExplicitDependencies {@link #requireExplicitDependencies} + * @since 3.12.0 + */ + public void setRequireExplicitDependencies(boolean requireExplicitDependencies) { + this.requireExplicitDependencies = requireExplicitDependencies; + } + /** * @param remoteRepositories {@link #remoteRepositories} * @since 3.12.0 @@ -454,6 +495,7 @@ public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMo } setArtifactItems(Collections.singletonList(artifactItem)); + artifactFromCommandLine = true; } } } diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcher.java b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcher.java new file mode 100644 index 00000000..0f90a3c4 --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcher.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.maven.plugins.dependency.fromConfiguration; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; +import org.apache.maven.model.Dependency; + +/** + * Checks that artifact items correspond to dependencies declared in the project, so that Maven sees them when it + * orders the reactor. + * <p> + * Items and dependencies are compared on groupId, artifactId, extension and classifier. The type is translated with + * the artifact handler, so {@code type=test-jar} equals {@code type=jar} with {@code classifier=tests}. The type + * defaults to {@code jar} and an empty classifier is no classifier. An item without a version matches regardless of + * the declared version. + */ +final class ExplicitDependencyMatcher { + private final ArtifactHandlerManager artifactHandlerManager; + + private final List<Dependency> declared; + + ExplicitDependencyMatcher(ArtifactHandlerManager artifactHandlerManager, List<Dependency> declared) { + this.artifactHandlerManager = artifactHandlerManager; + this.declared = declared == null ? new ArrayList<Dependency>() : declared; + } + + /** + * Returns one message per item that is not declared, or declared with a different version. + * + * @param items the items to check + * @return the problems, empty if every item is declared + */ + List<String> findViolations(List<ArtifactItem> items) { + List<String> violations = new ArrayList<>(); + for (ArtifactItem item : items) { + String violation = check(item); + if (violation != null) { + violations.add(violation); + } + } + return violations; + } + + private String check(ArtifactItem item) { + String extension = extension(item.getType()); + String classifier = classifier(item.getType(), item.getClassifier()); + String version = item.getVersion() == null ? "" : item.getVersion().trim(); + + List<String> declaredVersions = new ArrayList<>(); + for (Dependency dependency : declared) { + if (Objects.equals(item.getGroupId(), dependency.getGroupId()) + && Objects.equals(item.getArtifactId(), dependency.getArtifactId()) + && extension.equals(extension(dependency.getType())) + && Objects.equals(classifier, classifier(dependency.getType(), dependency.getClassifier()))) { + if (version.isEmpty() || version.equals(dependency.getVersion())) { + return null; + } + declaredVersions.add(String.valueOf(dependency.getVersion())); + } + } + if (declaredVersions.isEmpty()) { + return item + " is not declared as a dependency of the project"; + } + return item + " is declared as a dependency with version " + String.join(", ", declaredVersions) + ", not " + + version; + } + + private ArtifactHandler handler(String type) { + return artifactHandlerManager.getArtifactHandler(type == null || type.isEmpty() ? "jar" : type); + } + + private String extension(String type) { + ArtifactHandler handler = handler(type); + String extension = handler == null ? null : handler.getExtension(); + return extension != null ? extension : type == null || type.isEmpty() ? "jar" : type; + } + + private String classifier(String type, String classifier) { + if (classifier != null && !classifier.isEmpty()) { + return classifier; + } + ArtifactHandler handler = handler(type); + String fromHandler = handler == null ? null : handler.getClassifier(); + return fromHandler == null || fromHandler.isEmpty() ? null : fromHandler; + } +} diff --git a/src/site/markdown/usage.md.vm b/src/site/markdown/usage.md.vm index df994e25..90920ae1 100644 --- a/src/site/markdown/usage.md.vm +++ b/src/site/markdown/usage.md.vm @@ -38,6 +38,8 @@ This goal is meant to be bound to a lifecycle phase and configured in your `pom. The artifact version is optional. If not set, the plugin will attempt to resolve it from the project dependencies and then the dependencyManagement section. +Set `requireExplicitDependencies` (`-Dmdep.requireExplicitDependencies=true`) to fail the build when an artifact item is not declared in the `dependencies` of the project, or is declared with another version. Maven orders the modules of a reactor build by the declared dependencies only, so an undeclared item built by another module can be missing or stale. The check does not apply to the `artifact` command line parameter. + See the [Overwrite Rules](#Overwrite_Rules) section for rules about how overwriting is handled. Configure the plugin something like this if you intend to bind it to execute along with your build: @@ -194,6 +196,8 @@ See the [Overwrite Rules](#Overwrite_Rules) section for rules about how overwrit The artifact version is optional. If not set, the plugin will attempt to resolve it from the project dependencies and then the dependencyManagement section. +Set `requireExplicitDependencies` (`-Dmdep.requireExplicitDependencies=true`) to fail the build when an artifact item is not declared in the `dependencies` of the project, or is declared with another version. Maven orders the modules of a reactor build by the declared dependencies only, so an undeclared item built by another module can be missing or stale. The check does not apply to the `artifact` command line parameter. + Configure the plugin something like this if you intend to bind it to execute along with your build: ```xml diff --git a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcherTest.java b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcherTest.java new file mode 100644 index 00000000..f8e56cb8 --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcherTest.java @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.maven.plugins.dependency.fromConfiguration; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; +import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; +import org.apache.maven.model.Dependency; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class ExplicitDependencyMatcherTest { + private final ArtifactHandlerManager handlers = handlers(); + + private static ArtifactHandlerManager handlers() { + ArtifactHandlerManager manager = mock(ArtifactHandlerManager.class); + when(manager.getArtifactHandler(anyString())).thenAnswer(invocation -> { + String type = invocation.getArgument(0); + if ("test-jar".equals(type)) { + ArtifactHandler handler = mock(ArtifactHandler.class); + when(handler.getExtension()).thenReturn("jar"); + when(handler.getClassifier()).thenReturn("tests"); + return handler; + } + return new DefaultArtifactHandler(type); + }); + return manager; + } + + private static Dependency dependency(String artifactId, String version, String type, String classifier) { + Dependency dependency = new Dependency(); + dependency.setGroupId("g"); + dependency.setArtifactId(artifactId); + dependency.setVersion(version); + if (type != null) { + dependency.setType(type); + } + dependency.setClassifier(classifier); + return dependency; + } + + private static ArtifactItem item(String artifactId, String version, String type, String classifier) { + ArtifactItem item = new ArtifactItem(); + item.setGroupId("g"); + item.setArtifactId(artifactId); + item.setVersion(version); + if (type != null) { + item.setType(type); + } + item.setClassifier(classifier); + return item; + } + + private List<String> check(List<Dependency> declared, ArtifactItem... items) { + return new ExplicitDependencyMatcher(handlers, declared).findViolations(Arrays.asList(items)); + } + + @Test + void declaredItemMatches() { + assertTrue(check(Collections.singletonList(dependency("a", "1.0", null, null)), item("a", "1.0", null, null)) + .isEmpty()); + } + + @Test + void missingItemIsReported() { + List<String> violations = + check(Collections.singletonList(dependency("a", "1.0", null, null)), item("b", "1.0", null, null)); + assertEquals(1, violations.size()); + assertTrue(violations.get(0).contains("g:b:1.0:jar"), violations.get(0)); + assertTrue(violations.get(0).contains("not declared"), violations.get(0)); + } + + @Test + void emptyTypeAndClassifierAreDefaults() { + assertTrue(check(Collections.singletonList(dependency("a", "1.0", "jar", "")), item("a", "1.0", "", "")) + .isEmpty()); + } + + @Test + void typeMustMatch() { + assertEquals( + 1, + check(Collections.singletonList(dependency("a", "1.0", "jar", null)), item("a", "1.0", "zip", null)) + .size()); + } + + @Test + void classifierMustMatch() { + assertEquals( + 1, + check( + Collections.singletonList(dependency("a", "1.0", "jar", null)), + item("a", "1.0", "jar", "sources")) + .size()); + assertEquals( + 1, + check( + Collections.singletonList(dependency("a", "1.0", "jar", "sources")), + item("a", "1.0", "jar", null)) + .size()); + } + + @Test + void testJarEqualsJarWithTestsClassifier() { + assertTrue(check( + Collections.singletonList(dependency("a", "1.0", "test-jar", null)), + item("a", "1.0", "jar", "tests")) + .isEmpty()); + assertTrue(check( + Collections.singletonList(dependency("a", "1.0", "jar", "tests")), + item("a", "1.0", "test-jar", null)) + .isEmpty()); + assertEquals( + 1, + check( + Collections.singletonList(dependency("a", "1.0", "jar", null)), + item("a", "1.0", "test-jar", null)) + .size()); + } + + @Test + void versionMismatchIsReportedWithDeclaredVersion() { + List<String> violations = + check(Collections.singletonList(dependency("a", "1.0", null, null)), item("a", "2.0", null, null)); + assertEquals(1, violations.size()); + assertTrue(violations.get(0).contains("version 1.0, not 2.0"), violations.get(0)); + } + + @Test + void itemWithoutVersionMatchesAnyDeclaredVersion() { + assertTrue(check(Collections.singletonList(dependency("a", "1.0", null, null)), item("a", null, null, null)) + .isEmpty()); + } + + @Test + void anyDeclaredVariantWithTheVersionMatches() { + assertTrue(check( + Arrays.asList(dependency("a", "1.0", null, null), dependency("a", "2.0", null, null)), + item("a", "2.0", null, null)) + .isEmpty()); + } + + @Test + void everyViolationIsListed() { + assertEquals( + 2, + check(Collections.<Dependency>emptyList(), item("a", "1", null, null), item("b", "1", null, null)) + .size()); + } +}
