-
Notifications
You must be signed in to change notification settings - Fork 85
SHRINKRES-355 - Fix artifact selection in EmbeddedGradleImporter and … #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xjusko
wants to merge
1
commit into
shrinkwrap:master
Choose a base branch
from
xjusko:SHRINKRES-355
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
gradle/impl-gradle-embedded-archive/src/it/multiple-artifacts-sample/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| apply plugin: 'war' | ||
| apply plugin: 'java' | ||
|
|
||
| group = 'org.jboss.shrinkwrap.resolver.test' | ||
| version = '1.0.0' | ||
| description = "ShrinkWrap Resolver Gradle Test: Ambiguous Sample (JAR + WAR)" | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| // Minimal dependencies | ||
| dependencies { | ||
| testImplementation group: 'junit', name: 'junit', version: '4.12' | ||
| } | ||
|
|
||
| // Force generation of the JAR to create the ambiguity (JAR vs WAR) | ||
| jar { | ||
| enabled = true | ||
| } | ||
|
|
||
| // Ensure JAR is built alongside the WAR | ||
| assemble.dependsOn jar |
8 changes: 8 additions & 0 deletions
8
...adle-embedded-archive/src/it/multiple-artifacts-sample/src/main/java/testArq/Service.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package testArq; | ||
|
|
||
| public class Service { | ||
|
|
||
| public String greet() { | ||
| return "Hello from Service"; | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
...-gradle-embedded-archive/src/it/multiple-artifacts-sample/src/main/webapp/WEB-INF/web.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <web-app> | ||
| <distributable/> | ||
| </web-app> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| import java.io.File; | ||
| import java.net.URI; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import org.gradle.tooling.BuildLauncher; | ||
| import org.gradle.tooling.GradleConnector; | ||
|
|
@@ -32,13 +33,19 @@ | |
| import org.jboss.shrinkwrap.api.gradle.archive.importer.embedded.DistributionConfigurationStage; | ||
| import org.jboss.shrinkwrap.api.gradle.archive.importer.embedded.EmbeddedGradleImporter; | ||
| import org.jboss.shrinkwrap.api.importer.ZipImporter; | ||
| import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; | ||
| import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
| import org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive; | ||
| import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
| import org.jboss.shrinkwrap.impl.base.Validate; | ||
|
|
||
| /** | ||
| * @author <a href="mailto:mmatloka@gmail.com">Michal Matloka</a> | ||
| */ | ||
| public class EmbeddedGradleImporterImpl implements EmbeddedGradleImporter, DistributionConfigurationStage { | ||
|
|
||
| private static final Logger log = Logger.getLogger(EmbeddedGradleImporterImpl.class.getName()); | ||
|
|
||
| private static final String SAX_PARSER_FACTORY_KEY = "javax.xml.parsers.SAXParserFactory"; | ||
|
|
||
| private final Archive<?> archive; | ||
|
|
@@ -100,14 +107,14 @@ public DistributionConfigurationStage forThisProjectDirectory() { | |
| public <TYPE extends Assignable> TYPE as(final Class<TYPE> clazz) { | ||
| final File result; | ||
| if (buildResult == null) { | ||
| result = importFromDefaultLibsDirectory(); | ||
| result = importFromDefaultLibsDirectory(clazz); | ||
| } else { | ||
| result = importFromConfiguredPath(); | ||
| } | ||
| return ShrinkWrap.create(ZipImporter.class, archive.getName()).importFrom(result).as(clazz); | ||
| } | ||
|
|
||
| private File importFromDefaultLibsDirectory() { | ||
| private File importFromDefaultLibsDirectory(final Class<? extends Assignable> clazz) { | ||
| final GradleProject currentGradleProject = findCurrentGradleProject(); | ||
| final File buildDir = currentGradleProject.getBuildDirectory(); | ||
| final File libsDir = new File(buildDir, "libs"); | ||
|
|
@@ -118,6 +125,31 @@ private File importFromDefaultLibsDirectory() { | |
| "Wrong project directory is used. Tests have to be run from working directory which is a current sub-module directory."); | ||
| } | ||
|
|
||
| // When multiple artifacts are present (e.g., both .jar and .war), select the one | ||
| // matching the requested archive type to avoid importing the wrong artifact. | ||
| final String requiredExtension; | ||
| if (WebArchive.class.isAssignableFrom(clazz)) { | ||
| requiredExtension = ".war"; | ||
| } else if (EnterpriseArchive.class.isAssignableFrom(clazz)) { | ||
| requiredExtension = ".ear"; | ||
| } else if (ResourceAdapterArchive.class.isAssignableFrom(clazz)) { | ||
| requiredExtension = ".rar"; | ||
| } else if (JavaArchive.class.isAssignableFrom(clazz)) { | ||
| requiredExtension = ".jar"; | ||
| } else { | ||
| requiredExtension = null; | ||
| } | ||
|
|
||
| if (requiredExtension != null) { | ||
| for (File file : results) { | ||
| if (file.getName().endsWith(requiredExtension)) { | ||
| return file; | ||
| } | ||
| } | ||
| log.warning("No artifact with extension '" + requiredExtension + "' found in " + libsDir | ||
| + ", falling back to " + results[0].getName()); | ||
| } | ||
|
|
||
| return results[0]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh, this is preexisting but sloppy, since listFiles is nondeterministic... :-/ |
||
| } | ||
|
|
||
|
|
||
46 changes: 46 additions & 0 deletions
46
...mpl/gradle/archive/importer/embedded/MultipleArtifactsEmbeddedGradleImporterTestCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * JBoss, Home of Professional Open Source | ||
| * Copyright 2026, Red Hat Inc., and individual contributors | ||
| * by the @authors tag. See the copyright.txt in the distribution for a | ||
| * full listing of individual contributors. | ||
| * | ||
| * 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 org.jboss.shrinkwrap.impl.gradle.archive.importer.embedded; | ||
|
|
||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.shrinkwrap.api.gradle.archive.importer.embedded.EmbeddedGradleImporter; | ||
| import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Regression test for SHRINKRES-355. | ||
| * Verifies that the importer picks the correct artifact (.war) even when a .jar is present. | ||
| */ | ||
| public class MultipleArtifactsEmbeddedGradleImporterTestCase { | ||
|
|
||
| @Test | ||
| void shouldImportWarWhenJarIsAlsoPresent() { | ||
| final String dir = "src/it/multiple-artifacts-sample/"; | ||
|
|
||
| final WebArchive webArchive = ShrinkWrap.create(EmbeddedGradleImporter.class) | ||
| .forProjectDirectory(dir) | ||
| .importBuildOutput() | ||
| .as(WebArchive.class); | ||
|
|
||
| assertThat(webArchive.contains("WEB-INF/classes/testArq/Service.class")) | ||
| .as("Main sources should be inside WEB-INF/classes") | ||
| .isTrue(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like we should log a warning when nothing matches