diff --git a/liquibase-core/src/main/java/liquibase/integration/AbstractIntegration.java b/liquibase-core/src/main/java/liquibase/integration/AbstractIntegration.java new file mode 100644 index 00000000000..760142bfb3b --- /dev/null +++ b/liquibase-core/src/main/java/liquibase/integration/AbstractIntegration.java @@ -0,0 +1,4 @@ +package liquibase.integration; + +public class AbstractIntegration { +} diff --git a/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/AbstractLiquibaseChangeLogMojo.java b/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/AbstractLiquibaseChangeLogMojo.java index 17a9b7e3dc4..d32d10684e1 100644 --- a/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/AbstractLiquibaseChangeLogMojo.java +++ b/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/AbstractLiquibaseChangeLogMojo.java @@ -159,17 +159,18 @@ protected void printSettings(String indent) { @Override protected ResourceAccessor getResourceAccessor(ClassLoader cl) { - List resourceAccessors = new ArrayList(); - resourceAccessors.add(new MavenResourceAccessor(cl)); - resourceAccessors.add(new FileSystemResourceAccessor(project.getBasedir())); - resourceAccessors.add(new ClassLoaderResourceAccessor(getClass().getClassLoader())); - - if (changeLogDirectory != null) { - calculateChangeLogDirectoryAbsolutePath(); - resourceAccessors.add(new FileSystemResourceAccessor(new File(changeLogDirectory))); - } - - return new SearchPathResourceAccessor(searchPath, resourceAccessors.toArray(new ResourceAccessor[0])); +// List resourceAccessors = new ArrayList(); +// resourceAccessors.add(new MavenResourceAccessor(cl)); +// resourceAccessors.add(new FileSystemResourceAccessor(project.getBasedir())); +// resourceAccessors.add(new ClassLoaderResourceAccessor(getClass().getClassLoader())); +// +// if (changeLogDirectory != null) { +// calculateChangeLogDirectoryAbsolutePath(); +// resourceAccessors.add(new FileSystemResourceAccessor(new File(changeLogDirectory))); +// } +// +// return new SearchPathResourceAccessor(searchPath, resourceAccessors.toArray(new ResourceAccessor[0])); + return null; } @Override diff --git a/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/AbstractLiquibaseMojo.java b/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/AbstractLiquibaseMojo.java index 067bb694215..92aa3243961 100644 --- a/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/AbstractLiquibaseMojo.java +++ b/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/AbstractLiquibaseMojo.java @@ -893,16 +893,17 @@ private static InputStream handlePropertyFileInputStream(String propertyFile) th } protected ClassLoader getMavenArtifactClassLoader() throws MojoExecutionException { - try { - return MavenUtils.getArtifactClassloader(project, - includeArtifact, - includeTestOutputDirectory, - getClass(), - getLog(), - verbose); - } catch (MalformedURLException e) { - throw new MojoExecutionException("Failed to create artifact classloader", e); - } + return null; +// try { +// return MavenUtils.getArtifactClassloader(project, +// includeArtifact, +// includeTestOutputDirectory, +// getClass(), +// getLog(), +// verbose); +// } catch (MalformedURLException e) { +// throw new MojoExecutionException("Failed to create artifact classloader", e); +// } } /** @@ -926,9 +927,10 @@ protected ClassLoader getClassLoaderIncludingProjectClasspath() throws MojoExecu } protected ResourceAccessor getResourceAccessor(ClassLoader cl) { - ResourceAccessor mFO = new MavenResourceAccessor(cl); - ResourceAccessor fsFO = new FileSystemResourceAccessor(project.getBasedir()); - return new SearchPathResourceAccessor(searchPath, mFO, fsFO); +// ResourceAccessor mFO = new MavenResourceAccessor(cl); +// ResourceAccessor fsFO = new FileSystemResourceAccessor(project.getBasedir()); +// return new SearchPathResourceAccessor(searchPath, mFO, fsFO); + return null; } /** diff --git a/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/LiquibaseCommandMojo.java b/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/LiquibaseMojo.java similarity index 64% rename from liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/LiquibaseCommandMojo.java rename to liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/LiquibaseMojo.java index de3900da4c1..a13b65095dc 100644 --- a/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/LiquibaseCommandMojo.java +++ b/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/LiquibaseMojo.java @@ -3,16 +3,13 @@ import liquibase.Scope; import liquibase.command.CommandDefinition; import liquibase.command.CommandFactory; -import liquibase.command.CommandResults; import liquibase.command.CommandScope; import liquibase.integration.commandline.Main; -import liquibase.util.StringUtil; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.descriptor.Parameter; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.component.MapOrientedComponent; import org.codehaus.plexus.component.configurator.ComponentConfigurationException; @@ -26,45 +23,28 @@ import java.util.Map; import java.util.Properties; -public class LiquibaseCommandMojo extends AbstractMojo implements MapOrientedComponent { +public class LiquibaseMojo extends AbstractMojo implements MapOrientedComponent { - private String goal; private Map configuration; - private ClassLoader classloader; -// private List goalParameters; + private MavenProject project; + private MojoExecution mojoExecution; - public LiquibaseCommandMojo() { + public LiquibaseMojo() { } @Override public void addComponentRequirement(ComponentRequirement componentRequirement, Object o) throws ComponentConfigurationException { - System.out.println("Add compp"); + //TODO: How does this get used? } @Override - public void setComponentConfiguration(Map map) throws ComponentConfigurationException { - MojoExecution mojoExecution = (MojoExecution) map.get("mojoExecution"); - MavenProject project = (MavenProject) map.get("project"); - - this.goal = mojoExecution.getGoal(); - + public void setComponentConfiguration(Map config) throws ComponentConfigurationException { + this.mojoExecution = (MojoExecution) config.get("mojoExecution"); + this.project = (MavenProject) config.get("project"); try { - List classpathElements = project.getCompileClasspathElements(); - classpathElements.add(project.getBuild().getOutputDirectory()); - URL urls[] = new URL[classpathElements.size()]; - for (int i = 0; i < classpathElements.size(); ++i) { - urls[i] = new File((String) classpathElements.get(i)).toURI().toURL(); - } - this.classloader = new URLClassLoader(urls, MavenUtils.getArtifactClassloader(project, - true, - true, - getClass(), - getLog(), - false)); - this.configuration = new HashMap<>(); - for (Map.Entry entry : map.entrySet()) { + for (Map.Entry entry : config.entrySet()) { Object value = entry.getValue(); if (value == null) { continue; @@ -75,7 +55,7 @@ public void setComponentConfiguration(Map map) throws ComponentConfigurati this.configuration.put(String.valueOf(entry.getKey()), entry.getValue()); } - Properties userProperties = ((MavenSession) map.get("session")).getUserProperties(); + Properties userProperties = ((MavenSession) config.get("session")).getUserProperties(); for (Map.Entry entry : userProperties.entrySet()) { this.configuration.put(String.valueOf(entry.getKey()), entry.getValue()); } @@ -86,10 +66,9 @@ public void setComponentConfiguration(Map map) throws ComponentConfigurati @Override public void execute() throws MojoExecutionException, MojoFailureException { -// System.out.println("EXECUTE COMMAND MOJO!!!"+goal); -// System.out.println(StringUtil.join(configuration, ",")); Main.runningFromNewCli = true; + String goal = mojoExecution.getGoal(); try { CommandScope commandScope = new CommandScope(goal); @@ -103,7 +82,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { } Map scopeValues = new HashMap<>(); - scopeValues.put(Scope.Attr.resourceAccessor.name(), new MavenResourceAccessor(classloader)); + scopeValues.put(Scope.Attr.resourceAccessor.name(), new MavenResourceAccessor(project)); Scope.child(scopeValues, () -> { commandScope.execute(); }); diff --git a/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/MavenResourceAccessor.java b/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/MavenResourceAccessor.java index 70cb1cd0abf..91b4d1ff27b 100644 --- a/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/MavenResourceAccessor.java +++ b/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/MavenResourceAccessor.java @@ -1,19 +1,61 @@ package org.liquibase.maven.plugins; +import liquibase.Scope; import liquibase.resource.ClassLoaderResourceAccessor; +import liquibase.resource.CompositeResourceAccessor; +import liquibase.resource.FileSystemResourceAccessor; import liquibase.resource.InputStreamList; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.project.MavenProject; +import java.io.File; import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Iterator; +import java.util.List; +import java.util.Set; import java.util.SortedSet; +import static sun.awt.FontConfiguration.verbose; + /** * Extension of {@link liquibase.resource.ClassLoaderResourceAccessor} for Maven which will use a default or user specified {@link ClassLoader} to load files/resources. */ -public class MavenResourceAccessor extends ClassLoaderResourceAccessor { +public class MavenResourceAccessor extends CompositeResourceAccessor { + + public MavenResourceAccessor(MavenProject project) throws DependencyResolutionRequiredException { + for (String element : project.getCompileClasspathElements()) { + this.addResourceAccessor(new FileSystemResourceAccessor(new File(element))); + } + + Set dependencies = project.getArtifacts(); + if (dependencies != null) { + for (Artifact artifact : dependencies) { + this.addResourceAccessor(new FileSystemResourceAccessor(artifact.getFile())); + } + } else { + Scope.getCurrentScope().getLog(getClass()).fine("No artifacts for the Maven project to add to the searchPath"); + } + + // If the actual artifact can be resolved, then use that, otherwise use the build + // directory as that should contain the files for this project that we will need to + // run against. It is possible that the build directly could be empty, but we cannot + // directly include the source and resources as the resources may require filtering + // to replace any placeholders in the resource files. + File projectArtifactFile = project.getArtifact().getFile(); + if (projectArtifactFile == null) { + this.addResourceAccessor(new FileSystemResourceAccessor(new File(project.getBuild().getOutputDirectory()))); + } else { + this.addResourceAccessor(new FileSystemResourceAccessor(projectArtifactFile)); + } - public MavenResourceAccessor(ClassLoader classLoader) { - super(classLoader); +//TODO if (includeTestOutputDirectory) { + this.addResourceAccessor(new FileSystemResourceAccessor(new File(project.getBuild().getTestOutputDirectory()))); +// } } @Override diff --git a/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/MavenUtils.java b/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/MavenUtils.java index cddbed0196b..9595b011267 100644 --- a/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/MavenUtils.java +++ b/liquibase-maven-plugin/src/main/java/org/liquibase/maven/plugins/MavenUtils.java @@ -28,107 +28,6 @@ public class MavenUtils { public static final String LOG_SEPARATOR = "------------------------------------------------------------------------"; - /** - * Obtains a {@link ClassLoader} that can load from the Maven project dependencies. If - * the dependencies have not be resolved (or there are none) then this will just end up - * delegating to the parent {@link ClassLoader} of this class. - * - * @return The ClassLoader that can load the resolved dependencies for the Maven - * project. - * @throws java.net.MalformedURLException If any of the dependencies cannot be resolved - * into a URL. - */ - public static ClassLoader getArtifactClassloader(MavenProject project, - boolean includeArtifact, - boolean includeTestOutputDirectory, - Class clazz, - Log log, - boolean verbose) - throws MalformedURLException { - if (verbose) { - log.info("Loading artifacts into URLClassLoader"); - } - Set uris = new HashSet<>(); - // Find project dependencies, including the transitive ones. - Set dependencies = project.getArtifacts(); - if ((dependencies != null) && !dependencies.isEmpty()) { - for (Iterator it = dependencies.iterator(); it.hasNext(); ) { - addArtifact(uris, (Artifact) it.next(), log, verbose); - } - } else { - log.info("there are no resolved artifacts for the Maven project."); - } - - // Include the artifact for the actual maven project if requested - if (includeArtifact) { - // If the actual artifact can be resolved, then use that, otherwise use the build - // directory as that should contain the files for this project that we will need to - // run against. It is possible that the build directly could be empty, but we cannot - // directly include the source and resources as the resources may require filtering - // to replace any placeholders in the resource files. - Artifact a = project.getArtifact(); - if (a.getFile() != null) { - addArtifact(uris, a, log, verbose); - } else { - addFile(uris, new File(project.getBuild().getOutputDirectory()), log, verbose); - } - } - if (includeTestOutputDirectory) { - addFile(uris, new File(project.getBuild().getTestOutputDirectory()), log, verbose); - } - if (verbose) { - log.info(LOG_SEPARATOR); - } - - List uriList = new ArrayList<>(uris); - URL[] urlArray = new URL[uris.size()]; - for (int i = 0; i < uris.size(); i++) { - urlArray[i] = uriList.get(i).toURL(); - } - return new URLClassLoader(urlArray, clazz.getClassLoader()); - } - - /** - * Adds the artifact file into the set of URLs so it can be used in a URLClassLoader. - * - * @param urls The set to add the artifact file URL to. - * @param artifact The Artifact to resolve the file for. - * @throws MalformedURLException If there is a problem creating the URL for the file. - */ - private static void addArtifact(Set urls, - Artifact artifact, - Log log, - boolean verbose) - throws MalformedURLException { - File f = artifact.getFile(); - if (f == null) { - log.warn("Artifact with no actual file, '" + artifact.getGroupId() - + ":" + artifact.getArtifactId() + "'"); - } else { - addFile(urls, f, log, verbose); - } -// if (f != null) { -// URL fileURL = f.toURI().toURL(); -// if (verbose) { -// log.info(" artifact: " + fileURL); -// } -// urls.add(fileURL); -// } else { -// log.warning("Artifact with no actual file, '" + artifact.getGroupId() -// + ":" + artifact.getArtifactId() + "'"); -// } - } - - private static void addFile(Set urls, File f, Log log, boolean verbose) - throws MalformedURLException { - if (f != null) { - URI fileUri = f.toURI(); - if (verbose) { - log.info(" artifact: " + fileUri); - } - urls.add(fileUri); - } - } /** * Recursively searches for the field specified by the fieldName in the class and all