Re: [PR] Match maven-shared-archive-resources on the last path segment [maven-source-plugin]
kratos0718 closed pull request #317: Match maven-shared-archive-resources on the last path segment URL: https://github.com/apache/maven-source-plugin/pull/317 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
Re: [PR] Match maven-shared-archive-resources on the last path segment [maven-source-plugin]
kratos0718 commented on PR #317:
URL:
https://github.com/apache/maven-source-plugin/pull/317#issuecomment-5322850944
Closing this — the resource-root refactor on `master` has already fixed the
underlying problem, so the change is no longer needed.
`createArchiver()` now reads:
```java
projectManager
.getEnabledSourceRoots(project, ProjectScope.MAIN,
Language.RESOURCES)
.map(SourceRoot::directory)
.filter(directory ->
directory.endsWith("maven-shared-archive-resources"))
```
`SourceRoot.directory()` returns `java.nio.file.Path` (`public default
java.nio.file.Path directory();` in `maven-api-core`), so that `endsWith` is
**`Path.endsWith`**, not `String.endsWith`. `Path.endsWith` compares complete
name elements, which is exactly the last-segment match this PR was adding.
Confirmed the difference rather than assuming it:
| path | `String.endsWith` | `Path.endsWith` |
|---|---|---|
| `/project/target/maven-shared-archive-resources` | true | true |
| `/var/lib/tmp-maven-shared-archive-resources-extra` | false | false |
| `/var/lib/maven-shared-archive-resources-backup` | false | false |
| `/home/x/my-maven-shared-archive-resources` | **true** | **false** |
Worth noting for #307: the two examples in that issue (`…-extra`,
`…-backup`) would not actually have matched even under `String.endsWith`, since
the suffix comes after the target text. The real false positive was the
*prefix* case in the last row — a directory such as
`my-maven-shared-archive-resources`. Either way `Path.endsWith` handles it, so
**#307 can be closed as fixed by the refactor**.
Thanks for the review on this one — the inlining note was right and I've
applied the same preference since.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
Re: [PR] Match maven-shared-archive-resources on the last path segment [maven-source-plugin]
elharo commented on code in PR #317:
URL:
https://github.com/apache/maven-source-plugin/pull/317#discussion_r3720821963
##
src/test/java/org/apache/maven/plugins/source/AbstractSourceJarMojoTest.java:
##
@@ -0,0 +1,109 @@
+/*
+ * 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.source;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.maven.api.Project;
+import org.apache.maven.api.model.Build;
+import org.apache.maven.api.model.Resource;
+import org.apache.maven.api.plugin.Log;
+import org.codehaus.plexus.archiver.FileSet;
+import org.codehaus.plexus.archiver.jar.JarArchiver;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.ArgumentCaptor;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+class AbstractSourceJarMojoTest {
+
+private static final String SHARED_ARCHIVE_RESOURCES =
"maven-shared-archive-resources";
+
+@Test
+void testSharedArchiveResourcesDirectoryIsAdded(@TempDir Path tempDir)
throws Exception {
+Path sharedArchiveResources =
Files.createDirectory(tempDir.resolve(SHARED_ARCHIVE_RESOURCES));
+
+JarArchiver jarArchiver = mock(JarArchiver.class);
Review Comment:
can you use a real object instead of a mock?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
Re: [PR] Match maven-shared-archive-resources on the last path segment [maven-source-plugin]
kratos0718 commented on PR #317: URL: https://github.com/apache/maven-source-plugin/pull/317#issuecomment-5192118618 Both points addressed: the literal is inlined, and `AbstractSourceJarMojoTest` now covers which directories `createArchiver()` selects. The tests drive `createArchiver()` with a stubbed project and a mock `JarArchiver` and assert the file sets it receives — the exactly-named directory is added, a directory whose name merely ends with that text is not, and when both are present only the former is. I checked the last two against the previous `endsWith` version and both fail there, so they do pin the behaviour rather than just describing it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
Re: [PR] Match maven-shared-archive-resources on the last path segment [maven-source-plugin]
kratos0718 commented on PR #317: URL: https://github.com/apache/maven-source-plugin/pull/317#issuecomment-5185955506 Inlined, thanks — the constant wasn't earning its keep at one usage. The diff is now +4/-2 in the single method. `mvn test` still green (10 tests across `SourceJarMojoTest` and `TestSourceJarMojoTest`), spotless clean. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
Re: [PR] Match maven-shared-archive-resources on the last path segment [maven-source-plugin]
elharo commented on code in PR #317:
URL:
https://github.com/apache/maven-source-plugin/pull/317#discussion_r3715045657
##
src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java:
##
@@ -58,6 +58,12 @@ public abstract class AbstractSourceJarMojo implements Mojo {
private static final String[] DEFAULT_EXCLUDES = new String[] {};
+/**
+ * Name of the directory produced by maven-remote-resources-plugin whose
contents are added to
+ * the source archive. Matched against the last path segment, not the
whole path.
+ */
+private static final String SHARED_ARCHIVE_RESOURCES =
"maven-shared-archive-resources";
Review Comment:
inline this as it's only used in one place
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
[PR] Match maven-shared-archive-resources on the last path segment [maven-source-plugin]
kratos0718 opened a new pull request, #317:
URL: https://github.com/apache/maven-source-plugin/pull/317
Fixes #307
### Problem
`AbstractSourceJarMojo.createArchiver()` identified the shared archive
resources directory by calling `String.endsWith` on the entire path:
```java
if (r.getDirectory().endsWith("maven-shared-archive-resources")) {
```
Because that tests the whole path string rather than the final segment, any
resource directory whose path merely *ends with* those characters also matches
— for example `/var/lib/tmp-maven-shared-archive-resources-extra`, or a
directory a user happens to name `my-maven-shared-archive-resources`. Its
contents would then be added to the source archive as though it were the
directory produced by maven-remote-resources-plugin.
### Fix
Compare the last path segment instead, using `Path.getFileName()`, and reuse
the already-resolved `Path` for `addDirectory` rather than parsing the same
string a second time. `getFileName()` returns `null` for a root path, so that
case is guarded.
The literal is lifted into a named constant with a short comment recording
that it is matched against the last segment, so the next reader does not have
to infer the intent.
### Testing
`mvn test` — 10 tests across `SourceJarMojoTest` and
`TestSourceJarMojoTest`, all passing. No behaviour change for correctly-named
directories; the difference only shows on paths that previously matched by
suffix.
- [x] I hereby declare this contribution to be licenced under the [Apache
License Version 2.0, January 2004](https://www.apache.org/licenses/LICENSE-2.0)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
