-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use LiquibaseMojo as implemention class. Include Pro commands in plugin.xml DAT-12158 #12
Open
wwillard7800
wants to merge
3
commits into
main
Choose a base branch
from
DAT-12158
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
199 changes: 199 additions & 0 deletions
199
src/main/java/liquibase/sdk/maven/plugins/BuildPluginXmlMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,199 @@ | ||||||
package liquibase.sdk.maven.plugins; | ||||||
|
||||||
import org.apache.commons.lang3.StringUtils; | ||||||
import org.apache.maven.plugin.AbstractMojo; | ||||||
import org.apache.maven.plugin.MojoExecutionException; | ||||||
import org.apache.maven.plugin.MojoFailureException; | ||||||
import org.apache.maven.plugins.annotations.Mojo; | ||||||
import org.apache.maven.plugins.annotations.Parameter; | ||||||
import org.w3c.dom.Document; | ||||||
import org.w3c.dom.Element; | ||||||
|
||||||
import javax.xml.parsers.DocumentBuilderFactory; | ||||||
import javax.xml.transform.OutputKeys; | ||||||
import javax.xml.transform.Transformer; | ||||||
import javax.xml.transform.TransformerException; | ||||||
import javax.xml.transform.TransformerFactory; | ||||||
import javax.xml.transform.dom.DOMSource; | ||||||
import javax.xml.transform.stream.StreamResult; | ||||||
import java.io.File; | ||||||
import java.io.FileWriter; | ||||||
import java.io.IOException; | ||||||
import java.net.URL; | ||||||
import java.net.URLClassLoader; | ||||||
import java.util.SortedMap; | ||||||
import java.util.SortedSet; | ||||||
|
||||||
@Mojo(name = "build-plugin-xml") | ||||||
public class BuildPluginXmlMojo extends AbstractMojo { | ||||||
|
||||||
@Parameter(required = true) | ||||||
private File liquibaseClassesDir; | ||||||
|
||||||
@Parameter(required = false) | ||||||
private File liquibaseProClassesDir; | ||||||
|
||||||
@Parameter(required = true) | ||||||
private File outputFile; | ||||||
|
||||||
@Override | ||||||
public void execute() throws MojoExecutionException, MojoFailureException { | ||||||
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); | ||||||
try { | ||||||
if (!liquibaseClassesDir.exists()) { | ||||||
throw new MojoFailureException(liquibaseClassesDir.getAbsolutePath() + " does not exist"); | ||||||
} | ||||||
|
||||||
Document pluginXml = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); | ||||||
Element pluginElement = pluginXml.createElement("plugin"); | ||||||
pluginXml.appendChild(pluginElement); | ||||||
|
||||||
addNode("name", "liquibase-maven-plugin", pluginElement, pluginXml); | ||||||
addNode("description", "A Maven plugin wraps up some of the functionality of Liquibase", pluginElement, pluginXml); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably should change the description now:
Suggested change
|
||||||
addNode("groupId", "org.liquibase", pluginElement, pluginXml); | ||||||
addNode("artifactId", "liquibase-maven-plugin", pluginElement, pluginXml); | ||||||
addNode("version", "0-SNAPSHOT", pluginElement, pluginXml); | ||||||
addNode("goalPrefix", "liquibase", pluginElement, pluginXml); | ||||||
addNode("isolatedRealm", "false", pluginElement, pluginXml); | ||||||
addNode("inheritedByDefault", "true", pluginElement, pluginXml); | ||||||
|
||||||
Element mojosElement = pluginXml.createElement("mojos"); | ||||||
pluginElement.appendChild(mojosElement); | ||||||
|
||||||
URLClassLoader classloader; | ||||||
if (liquibaseProClassesDir != null && liquibaseProClassesDir.exists()) { | ||||||
classloader = | ||||||
new URLClassLoader(new URL[]{ | ||||||
liquibaseClassesDir.toURI().toURL(), | ||||||
liquibaseProClassesDir.toURI().toURL() | ||||||
}); | ||||||
} else { | ||||||
classloader = | ||||||
new URLClassLoader(new URL[]{ | ||||||
liquibaseClassesDir.toURI().toURL() | ||||||
}); | ||||||
|
||||||
} | ||||||
|
||||||
Thread.currentThread().setContextClassLoader(classloader); | ||||||
Object scope = classloader.loadClass("liquibase.Scope") | ||||||
.getMethod("getCurrentScope").invoke(null); | ||||||
|
||||||
Class<?> commandFactoryClass = classloader.loadClass("liquibase.command.CommandFactory"); | ||||||
|
||||||
Object commandFactory = scope.getClass().getMethod("getSingleton", Class.class).invoke(scope, commandFactoryClass); | ||||||
|
||||||
SortedSet commands = (SortedSet) commandFactory.getClass().getMethod("getCommands", boolean.class).invoke(commandFactory, false); | ||||||
for (Object commandDef : commands) { | ||||||
String[] commandName = (String[]) commandDef.getClass().getMethod("getName").invoke(commandDef); | ||||||
System.out.println("See command " + StringUtils.join(commandName, " ")); | ||||||
|
||||||
Element mojoElement = pluginXml.createElement("mojo"); | ||||||
mojosElement.appendChild(mojoElement); | ||||||
|
||||||
for (int i = 0; i < commandName.length; i++) { | ||||||
if (i > 0) { | ||||||
commandName[i] = commandName[i].substring(0, 1).toUpperCase() + commandName[i].substring(1); | ||||||
} | ||||||
} | ||||||
String finalName = StringUtils.join(commandName); | ||||||
|
||||||
addNode("goal", finalName, mojoElement, pluginXml); | ||||||
addNode("description", (String) commandDef.getClass().getMethod("getLongDescription").invoke(commandDef), mojoElement, pluginXml); | ||||||
addNode("requiresDependencyResolution", "test", mojoElement, pluginXml); | ||||||
addNode("requiresDirectInvocation", "false", mojoElement, pluginXml); | ||||||
addNode("requiresProject", "true", mojoElement, pluginXml); | ||||||
addNode("requiresReports", "false", mojoElement, pluginXml); | ||||||
addNode("aggregator", "false", mojoElement, pluginXml); | ||||||
addNode("requiresOnline", "false", mojoElement, pluginXml); | ||||||
addNode("inheritedByDefault", "true", mojoElement, pluginXml); | ||||||
addNode("implementation", "org.liquibase.maven.plugins.LiquibaseMojo", mojoElement, pluginXml); | ||||||
addNode("language", "java", mojoElement, pluginXml); | ||||||
addNode("instantiationStrategy", "per-lookup", mojoElement, pluginXml); | ||||||
addNode("executionStrategy", "once-per-session", mojoElement, pluginXml); | ||||||
addNode("threadSafe", "false", mojoElement, pluginXml); | ||||||
addNode("configurator", "map-oriented", mojoElement, pluginXml); | ||||||
|
||||||
Element parametersElement = pluginXml.createElement("parameters"); | ||||||
mojoElement.appendChild(parametersElement); | ||||||
|
||||||
Element configurationElement = pluginXml.createElement("configuration"); | ||||||
mojoElement.appendChild(configurationElement); | ||||||
|
||||||
addMavenProperty("mojoExecution", "org.apache.maven.plugin.MojoExecution", "${mojoExecution}", parametersElement, configurationElement, pluginXml); | ||||||
addMavenProperty("session", "org.apache.maven.execution.MavenSession", "${session}", parametersElement, configurationElement, pluginXml); | ||||||
addMavenProperty("project", "org.apache.maven.project.MavenProject", "${project}", parametersElement, configurationElement, pluginXml); | ||||||
|
||||||
|
||||||
SortedMap arguments = (SortedMap) commandDef.getClass().getMethod("getArguments").invoke(commandDef); | ||||||
for (Object argDef : arguments.values()) { | ||||||
Element parameterElement = pluginXml.createElement("parameter"); | ||||||
parametersElement.appendChild(parameterElement); | ||||||
|
||||||
String argName = (String) argDef.getClass().getMethod("getName").invoke(argDef); | ||||||
String dataType = ((Class) argDef.getClass().getMethod("getDataType").invoke(argDef)).getName(); | ||||||
|
||||||
addNode("name", argName, parameterElement, pluginXml); | ||||||
addNode("type", dataType, parameterElement, pluginXml); | ||||||
addNode("required", String.valueOf(argDef.getClass().getMethod("isRequired").invoke(argDef)), parameterElement, pluginXml); | ||||||
addNode("editable", "true", parameterElement, pluginXml); | ||||||
addNode("description", (String) argDef.getClass().getMethod("getDescription").invoke(argDef), parameterElement, pluginXml); | ||||||
|
||||||
Element confElement = pluginXml.createElement(argName); | ||||||
configurationElement.appendChild(confElement); | ||||||
confElement.setAttribute("implementation", dataType); | ||||||
confElement.setTextContent("${liquibase.command." + StringUtils.join(commandName, ".") + "." + argName + "}"); | ||||||
String defaultValue = (String) argDef.getClass().getMethod("getDefaultValueDescription").invoke(argDef); | ||||||
if (defaultValue != null) { | ||||||
confElement.setAttribute("default-value", defaultValue); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
writeXml(pluginXml); | ||||||
} catch (Exception e) { | ||||||
throw new MojoExecutionException(e.getMessage(), e); | ||||||
} finally { | ||||||
Thread.currentThread().setContextClassLoader(originalClassLoader); | ||||||
} | ||||||
|
||||||
System.out.println("GENERATE PLUGIN.XML"); | ||||||
} | ||||||
|
||||||
private void addMavenProperty(String name, String type, String defaultValue, Element parametersElement, Element configurationElement, Document pluginXml) { | ||||||
Element thisParameterElement = pluginXml.createElement("parameter"); | ||||||
parametersElement.appendChild(thisParameterElement); | ||||||
addNode("name", name, thisParameterElement, pluginXml); | ||||||
addNode("type", type, thisParameterElement, pluginXml); | ||||||
addNode("required", "true", thisParameterElement, pluginXml); | ||||||
addNode("editable", "false", thisParameterElement, pluginXml); | ||||||
|
||||||
Element thisConfigElement = pluginXml.createElement(name); | ||||||
configurationElement.appendChild(thisConfigElement); | ||||||
thisConfigElement.setAttribute("implementation", type); | ||||||
thisConfigElement.setAttribute("default-value", defaultValue); | ||||||
|
||||||
} | ||||||
|
||||||
private void writeXml(Document pluginXml) throws IOException, TransformerException { | ||||||
DOMSource domSource = new DOMSource(pluginXml); | ||||||
TransformerFactory factory = TransformerFactory.newInstance(); | ||||||
Transformer transformer = factory.newTransformer(); | ||||||
factory.setAttribute("indent-number", 4); | ||||||
|
||||||
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); | ||||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | ||||||
try (FileWriter fileWriter = new FileWriter(outputFile)) { | ||||||
StreamResult sr = new StreamResult(fileWriter); | ||||||
transformer.transform(domSource, sr); | ||||||
} | ||||||
} | ||||||
|
||||||
private Element addNode(String nodeName, String text, Element parent, Document document) { | ||||||
Element newElement = document.createElement(nodeName); | ||||||
newElement.setTextContent(text); | ||||||
parent.appendChild(newElement); | ||||||
|
||||||
return newElement; | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some javadoc to this class that explains what it does.