Skip to content

Match maven-shared-archive-resources on the last path segment - #317

Open
kratos0718 wants to merge 3 commits into
apache:masterfrom
kratos0718:fix/307-exact-shared-archive-resources-match
Open

Match maven-shared-archive-resources on the last path segment#317
kratos0718 wants to merge 3 commits into
apache:masterfrom
kratos0718:fix/307-exact-shared-archive-resources-match

Conversation

@kratos0718

@kratos0718 kratos0718 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #307

Problem

AbstractSourceJarMojo.createArchiver() identified the shared archive resources directory by calling String.endsWith on the entire path:

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.

Testing

Which directories createArchiver() selects was not covered, so AbstractSourceJarMojoTest adds three cases. Each drives createArchiver() with a stubbed project and a mock JarArchiver, then asserts the file sets the archiver receives:

  • a directory named maven-shared-archive-resources is added
  • a directory whose name merely ends with that text is not added
  • when both are present, only the former is added

The last two fail against the previous suffix match and pass with the fix. mvn verify -DskipITs: 13 tests, all passing.

createArchiver() identified the shared archive resources directory with
String.endsWith on the whole path, so any resource directory whose path
merely ends with those characters matched too - for example
/var/lib/tmp-maven-shared-archive-resources-extra would be treated as
the shared directory and its contents added to the source archive.

Compare the last path segment instead, via Path.getFileName(), and reuse
the resolved Path for addDirectory rather than parsing the string twice.
getFileName() returns null for a root path, so that case is guarded.

Fixes apache#307
* 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";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline this as it's only used in one place

Used in only one place, so the constant did not earn its keep.
@kratos0718

Copy link
Copy Markdown
Contributor Author

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.

@elharo elharo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests required

createArchiver() adds a resource directory to the source archive only
when that directory is the one produced by maven-remote-resources-plugin.
Nothing covered which directories it selects, so neither the previous
suffix match nor the last-segment match was pinned by a test.

Drive createArchiver() with a stubbed project and a mock JarArchiver and
assert the file sets it receives: the directory named
maven-shared-archive-resources is added, a directory whose name merely
ends with that text is not, and when both are present only the former is
added. The latter two fail against the suffix match.
@kratos0718

Copy link
Copy Markdown
Contributor Author

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.

@elharo elharo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mocks means you're not actually testing all that much, and tests afre pretty tuightly coupled to implementation. Consider using an integration test that uses real files and real objects.

void testSharedArchiveResourcesDirectoryIsAdded(@TempDir Path tempDir) throws Exception {
Path sharedArchiveResources = Files.createDirectory(tempDir.resolve(SHARED_ARCHIVE_RESOURCES));

JarArchiver jarArchiver = mock(JarArchiver.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use a real object instead of a mock?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fragile endsWith check for maven-shared-archive-resources in createArchiver

2 participants