Maven: use relative directory path in the MavenResourceFileFilter and append the file name to get the file relative path

This commit is contained in:
Vladislav.Soroka
2017-08-18 15:38:46 +03:00
parent 1c128ddd45
commit 2efdd023ea
2 changed files with 16 additions and 20 deletions
@@ -73,18 +73,14 @@ public class MavenWebArtifactRootCopyingHandlerProvider extends ArtifactRootCopy
}
ResourceRootConfiguration rootConfiguration = artifactResourceConfiguration.getRootConfiguration(root);
String relativePathInWar = null;
String relativeDirInWar = null;
if (rootConfiguration == null) {
if (artifact.getOutputPath() != null &&
!FileUtil.isAncestor(new File(artifactResourceConfiguration.warSourceDirectory), root, false)) {
relativePathInWar = FileUtil.getRelativePath(new File(artifact.getOutputPath()), new File(targetDirectory, root.getName()));
if (relativePathInWar != null) {
// cut the exploded directory name
relativePathInWar = StringUtil.substringAfter(relativePathInWar, File.separator);
}
if (relativePathInWar == null) return null;
relativeDirInWar = FileUtil.getRelativePath(new File(artifact.getOutputPath()), targetDirectory);
if (relativeDirInWar == null) return null;
}
return new MavenWebArtifactCopyingHandler(artifactResourceConfiguration, moduleResourceConfiguration, relativePathInWar);
return new MavenWebArtifactCopyingHandler(artifactResourceConfiguration, moduleResourceConfiguration, relativeDirInWar);
}
MavenResourceFileProcessor fileProcessor = new MavenResourceFileProcessor(projectConfiguration, model.getProject(), moduleResourceConfiguration);
@@ -98,14 +94,14 @@ public class MavenWebArtifactRootCopyingHandlerProvider extends ArtifactRootCopy
public MavenWebArtifactCopyingHandler(@NotNull MavenWebArtifactConfiguration artifactConfig,
@NotNull MavenModuleResourceConfiguration moduleResourceConfig,
@Nullable String relativePath) {
this(getWarRootConfig(artifactConfig, moduleResourceConfig), moduleResourceConfig, relativePath);
@Nullable String relativeDirectoryPath) {
this(getWarRootConfig(artifactConfig, moduleResourceConfig), moduleResourceConfig, relativeDirectoryPath);
}
private MavenWebArtifactCopyingHandler(@NotNull ResourceRootConfiguration warRootConfig,
@NotNull MavenModuleResourceConfiguration moduleResourceConfig,
@Nullable String relativePath) {
this(new MavenResourceFileFilter(new File(toSystemDependentName(warRootConfig.directory)), warRootConfig, relativePath),
@Nullable String relativeDirectoryPath) {
this(new MavenResourceFileFilter(new File(toSystemDependentName(warRootConfig.directory)), warRootConfig, relativeDirectoryPath),
warRootConfig, moduleResourceConfig);
}
@@ -24,27 +24,27 @@ import java.io.File;
import java.io.FileFilter;
/**
* @author nik
*/
* @author nik
*/
public class MavenResourceFileFilter implements FileFilter {
private File myRoot;
private String myRelativePath;
private String myRelativeDirectoryPath;
private MavenPatternFileFilter myMavenPatternFileFilter;
public MavenResourceFileFilter(@NotNull File rootFile, @NotNull FilePattern filePattern) {
this(rootFile, filePattern, null);
}
public MavenResourceFileFilter(@NotNull File rootFile, @NotNull FilePattern filePattern, @Nullable String relativePath) {
public MavenResourceFileFilter(@NotNull File rootFile, @NotNull FilePattern filePattern, @Nullable String relativeDirectoryPath) {
myMavenPatternFileFilter = new MavenPatternFileFilter(filePattern.includes, filePattern.excludes);
myRoot = rootFile;
myRelativePath = relativePath;
myRelativeDirectoryPath = relativeDirectoryPath;
}
@Override
public boolean accept(@NotNull File file) {
String relPath = myRelativePath != null && myRelativePath.endsWith(file.getName()) ?
myRelativePath : FileUtil.getRelativePath(myRoot, file);
return relPath != null && myMavenPatternFileFilter.accept(relPath);
String relativeFilePath = myRelativeDirectoryPath != null ?
myRelativeDirectoryPath + File.separator + file.getName() : FileUtil.getRelativePath(myRoot, file);
return relativeFilePath != null && myMavenPatternFileFilter.accept(relativeFilePath);
}
}