Skip to content

Commit

Permalink
[MEAR-267] - Fixed detection if EAR JAR module is included into class…
Browse files Browse the repository at this point in the history
…path of particular EAR module manifest
  • Loading branch information
mabrarov committed Oct 3, 2020
1 parent 1670e8b commit 968d1f2
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions src/main/java/org/apache/maven/plugins/ear/EarMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -56,6 +57,7 @@
import org.apache.maven.shared.filtering.MavenFilteringException;
import org.apache.maven.shared.filtering.MavenResourcesExecution;
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
import org.apache.maven.shared.mapping.MappingUtils;
import org.apache.maven.shared.utils.io.FileUtils;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
Expand All @@ -70,6 +72,7 @@
import org.codehaus.plexus.archiver.zip.ZipArchiver;
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
import org.codehaus.plexus.components.io.filemappers.FileMapper;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.StringUtils;

Expand All @@ -84,6 +87,16 @@
public class EarMojo
extends AbstractEarMojo
{
/**
* File name mappings which can be used by elements of Class-Path manifest entry of EAR modules to identify if
* classpath element matches particular EAR module.
*/
private static final List<String> CLASSPATH_ELEMENT_FILE_NAME_MAPPINGS = Arrays.asList(
"@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@",
"@{groupId}@-@{artifactId}@-@{version}@@{dashClassifier?}@.@{extension}@",
"@{artifactId}@-@{baseVersion}@@{dashClassifier?}@.@{extension}@",
"@{groupId}@-@{artifactId}@-@{baseVersion}@@{dashClassifier?}@.@{extension}@" );

/**
* Single directory for extra files to include in the EAR.
*/
Expand Down Expand Up @@ -845,9 +858,10 @@ private void changeManifestClasspath( EarModule module, File original, JavaEEVer
if ( o instanceof JarModule )
{
JarModule jm = (JarModule) o;
if ( classPathElements.contains( jm.getBundleFileName() ) )
final int moduleClassPathIndex = findModuleInClassPathElements( classPathElements, jm );
if ( moduleClassPathIndex != -1 )
{
classPathElements.set( classPathElements.indexOf( jm.getBundleFileName() ), jm.getUri() );
classPathElements.set( moduleClassPathIndex, jm.getUri() );
}
else
{
Expand Down Expand Up @@ -946,4 +960,42 @@ private void deleteOutdatedResources( final Collection<String> outdatedResources
}
}
}

/**
* Searches JAR module in the list of classpath elements.
*
* @param classPathElements classpath elements to search among.
* @param module module to find among classpath elements defined by {@code classPathElements}
* @return -1 if {@code module} was not found in {@code classPathElements} or index of item of
* {@code classPathElements} which matches {@code module}
*/
private int findModuleInClassPathElements( final List<String> classPathElements, final JarModule module )
{
int moduleClassPathIndex = classPathElements.indexOf( module.getBundleFileName() );
if ( moduleClassPathIndex != -1 )
{
return moduleClassPathIndex;
}
// Check the case when classpath entry uses some of default file name mappings
// instead of file name mapping configured for EAR plugin
final Artifact artifact = module.getArtifact();
for ( String fileNameMapping : CLASSPATH_ELEMENT_FILE_NAME_MAPPINGS )
{
try
{
final String bundleFileName = MappingUtils.evaluateFileNameMapping( fileNameMapping, artifact );
moduleClassPathIndex = classPathElements.indexOf( bundleFileName );
if ( moduleClassPathIndex != -1 )
{
return moduleClassPathIndex;
}
}
catch ( InterpolationException e )
{
getLog().warn( "Failed to build bundle file name for artifact [" + module
+ "] using file name mapping: " + fileNameMapping, e );
}
}
return -1;
}
}

0 comments on commit 968d1f2

Please sign in to comment.