Skip to content

Commit

Permalink
Dynamic maven plugin runs
Browse files Browse the repository at this point in the history
  • Loading branch information
nvoxland committed Sep 14, 2022
1 parent c3b320c commit 73063b3
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 22 deletions.
64 changes: 42 additions & 22 deletions liquibase-maven-plugin/pom.xml
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>liquibase-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<packaging>jar</packaging>
<description>A Maven plugin wraps up some of the functionality of Liquibase</description>

<properties>
Expand Down Expand Up @@ -75,23 +75,41 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.4</version>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-sdk-maven-plugin</artifactId>
<version>0-SNAPSHOT</version>
<executions>
<execution>
<id>generated-helpmojo</id>
<id>default-liquibase-build</id>
<phase>generate-resources</phase>
<goals>
<goal>helpmojo</goal>
<goal>build-plugin-xml</goal>
</goals>
<configuration>
<liquibaseClassesDir>../liquibase-core/target/classes</liquibaseClassesDir>
<outputFile>src/main/resources/META-INF/maven/plugin.xml</outputFile>
</configuration>
</execution>
</executions>
<configuration>
<mojoDependencies>
<param>org.liquibase:liquibase-maven-plugin</param>
</mojoDependencies>
</configuration>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-plugin-plugin</artifactId>-->
<!-- <version>3.6.4</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>generated-helpmojo</id>-->
<!-- <goals>-->
<!-- <goal>helpmojo</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- <configuration>-->
<!-- <mojoDependencies>-->
<!-- <param>org.liquibase:liquibase-maven-plugin</param>-->
<!-- </mojoDependencies>-->
<!-- </configuration>-->
<!-- </plugin>-->

<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -116,7 +134,8 @@
<createTestSourcesJar>false</createTestSourcesJar>
<keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<transformer
implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<!-- Use a hand-crafted pom for distribution in this jar vs. what gets generated by maven -->
<resource>META-INF/maven/org.liquibase/liquibase-maven-plugin/pom.xml</resource>
<file>${basedir}/src/main/maven/release.pom.xml</file>
Expand Down Expand Up @@ -153,7 +172,8 @@
<configuration>
<artifactId>liquibase-maven-plugin</artifactId>
<file>${project.build.directory}/liquibase-maven-plugin-${project.version}.jar</file>
<sources>${project.build.directory}/liquibase-maven-plugin-${project.version}-sources.jar</sources>
<sources>${project.build.directory}/liquibase-maven-plugin-${project.version}-sources.jar
</sources>
<pomFile>${basedir}/src/main/maven/release.pom.xml</pomFile>
</configuration>
<executions>
Expand All @@ -172,14 +192,14 @@

</profiles>

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.4</version>
</plugin>
</plugins>
</reporting>
<!-- <reporting>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-plugin-plugin</artifactId>-->
<!-- <version>3.6.4</version>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </reporting>-->
</project>

@@ -0,0 +1,119 @@
package org.liquibase.maven.plugins;

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;
import org.codehaus.plexus.component.repository.ComponentRequirement;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class LiquibaseCommandMojo extends AbstractMojo implements MapOrientedComponent {

private String goal;
private Map<String, Object> configuration;
private ClassLoader classloader;
// private List<Parameter> goalParameters;

public LiquibaseCommandMojo() {
}

@Override
public void addComponentRequirement(ComponentRequirement componentRequirement, Object o) throws ComponentConfigurationException {
System.out.println("Add compp");
}

@Override
public void setComponentConfiguration(Map<?, ?> map) throws ComponentConfigurationException {
MojoExecution mojoExecution = (MojoExecution) map.get("mojoExecution");
MavenProject project = (MavenProject) map.get("project");

this.goal = mojoExecution.getGoal();


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()) {
Object value = entry.getValue();
if (value == null) {
continue;
}
if (value.getClass().getName().startsWith("org.apache")) {
continue;
}
this.configuration.put(String.valueOf(entry.getKey()), entry.getValue());
}

Properties userProperties = ((MavenSession) map.get("session")).getUserProperties();
for (Map.Entry<Object, Object> entry : userProperties.entrySet()) {
this.configuration.put(String.valueOf(entry.getKey()), entry.getValue());
}
} catch (Exception e) {
throw new ComponentConfigurationException(e.getMessage(), e);
}
}

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// System.out.println("EXECUTE COMMAND MOJO!!!"+goal);
// System.out.println(StringUtil.join(configuration, ","));
Main.runningFromNewCli = true;

try {
CommandScope commandScope = new CommandScope(goal);

CommandFactory commandFactory = Scope.getCurrentScope().getSingleton(CommandFactory.class);
CommandDefinition commandDef = commandFactory.getCommandDefinition(goal);
for (String argName : commandDef.getArguments().keySet()) {
Object value = configuration.get(argName);
if (value != null) {
commandScope.addArgumentValue(argName, value);
}
}

Map<String, Object> scopeValues = new HashMap<>();
scopeValues.put(Scope.Attr.resourceAccessor.name(), new MavenResourceAccessor(classloader));
Scope.child(scopeValues, () -> {
commandScope.execute();
});
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}


// public static void setField(Object field) {
// System.out.println("setting");
// }
}
@@ -0,0 +1,26 @@
package org.liquibase.maven.plugins;

import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import org.codehaus.plexus.component.configurator.ComponentConfigurator;
import org.codehaus.plexus.component.configurator.ConfigurationListener;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.configuration.PlexusConfiguration;

public class LiquibaseComponentConfigurator implements ComponentConfigurator {

@Override
public void configureComponent(Object o, PlexusConfiguration plexusConfiguration, ClassRealm classRealm) throws ComponentConfigurationException {

}

@Override
public void configureComponent(Object o, PlexusConfiguration plexusConfiguration, ExpressionEvaluator expressionEvaluator, ClassRealm classRealm) throws ComponentConfigurationException {

}

@Override
public void configureComponent(Object o, PlexusConfiguration plexusConfiguration, ExpressionEvaluator expressionEvaluator, ClassRealm classRealm, ConfigurationListener configurationListener) throws ComponentConfigurationException {

}
}

0 comments on commit 73063b3

Please sign in to comment.