Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<web-app>
<distributable/>
</web-app>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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());
}
Comment on lines 143 to 151
Copy link
Member

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


return results[0];
Copy link
Member

Choose a reason for hiding this comment

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

Uh, this is preexisting but sloppy, since listFiles is nondeterministic... :-/

}

Expand Down
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();
}
}