From 04fe0678d75283bdae36efb22d7bafdebcebbbb7 Mon Sep 17 00:00:00 2001 From: Martin Bergljung Date: Mon, 12 Sep 2016 16:57:02 +0100 Subject: [PATCH] Support for MySQL, PostgreSQL, and custom Tomcat dependencies, #400 --- .../org/alfresco/maven/plugin/RunMojo.java | 77 +++++++++++++-- .../maven/plugin/config/MavenDependency.java | 94 +++++++++++++++++++ .../plugin/{ => config}/ModuleDependency.java | 80 ++++++---------- .../maven/plugin/config/TomcatDependency.java | 61 ++++++++++++ 4 files changed, 256 insertions(+), 56 deletions(-) create mode 100644 plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/MavenDependency.java rename plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/{ => config}/ModuleDependency.java (61%) create mode 100644 plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/TomcatDependency.java diff --git a/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/RunMojo.java b/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/RunMojo.java index 393b5e74..8d180ea5 100644 --- a/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/RunMojo.java +++ b/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/RunMojo.java @@ -17,6 +17,8 @@ */ package org.alfresco.maven.plugin; +import org.alfresco.maven.plugin.config.ModuleDependency; +import org.alfresco.maven.plugin.config.TomcatDependency; import org.apache.commons.lang.StringUtils; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Dependency; @@ -112,9 +114,21 @@ public class RunMojo extends AbstractMojo { * Switch to enable/disable the H2 database when running embedded Tomcat. * This also brings in the needed H2 database scripts. */ - @Parameter(property = "maven.alfresco.enableH2", defaultValue = "true") + @Parameter(property = "maven.alfresco.enableH2", defaultValue = "false") protected boolean enableH2; + /** + * Switch to enable/disable the MySQL database when running embedded Tomcat. + */ + @Parameter(property = "maven.alfresco.enableMySQL", defaultValue = "false") + protected boolean enableMySQL; + + /** + * Switch to enable/disable the PostgreSQL database when running embedded Tomcat. + */ + @Parameter(property = "maven.alfresco.enablePostgreSQL", defaultValue = "false") + protected boolean enablePostgreSQL; + /** * Switch to enable/disable the Platform/Repository (alfresco.war) when running embedded Tomcat. */ @@ -175,6 +189,14 @@ public class RunMojo extends AbstractMojo { @Parameter(property = "maven.alfresco.edition", defaultValue = ALFRESCO_COMMUNITY_EDITION) protected String alfrescoEdition; + /** + * Tomcat Dependencies that should be added to the Embedded Tomcat configuration before start. + * Normally there would not be any extra dependencies, but could be if you run an Enterprise database + * such as Oracle, for which there's no quick config, such as enableH2, enableMySQL, or enablePostgreSQL. + */ + @Parameter(property = "maven.alfresco.tomcat.dependencies", defaultValue = "") + protected List tomcatDependencies; + /** * Maven GAV properties for standard Alfresco web applications. */ @@ -250,6 +272,7 @@ public class RunMojo extends AbstractMojo { } if (startTomcat) { + checkDatabaseConfig(); startTomcat(); } } @@ -549,7 +572,7 @@ public class RunMojo extends AbstractMojo { List ampModules = new ArrayList<>(); List jarModules = new ArrayList<>(); - if (modules != null) { + if (modules != null && modules.size() > 0) { for (ModuleDependency moduleDep : modules) { Element el = element(name("artifactItem"), element(name("groupId"), moduleDep.getGroupId()), @@ -694,6 +717,30 @@ public class RunMojo extends AbstractMojo { return warArtifactId; } + /** + * Check that a database configuration has been supplied correctly + */ + private void checkDatabaseConfig() throws MojoExecutionException { + if (enableH2 && !enableMySQL && !enablePostgreSQL) { + // Run with the H2 database + return; + } else if (!enableH2 && enableMySQL && !enablePostgreSQL) { + // Run with the MySQL database + return; + } else if (!enableH2 && !enableMySQL && enablePostgreSQL) { + // Run with the PostgreSQL database + return; + } else if (!enableH2 && !enableMySQL && !enablePostgreSQL) { + // Run with a database configured via Tomcat Dependencies + return; + } else { + throw new MojoExecutionException( + "Invalid database configuration, " + + "should be enableH2 or enableMySQL or enablePostgreSQL " + + "or none (i.e. config via Tomcat Dependencies)"); + } + } + /** * Start up the embedded Tomcat server with the webapps that has been * configured in the SDK project. @@ -703,27 +750,43 @@ public class RunMojo extends AbstractMojo { protected void startTomcat() throws MojoExecutionException { getLog().info("Starting Tomcat"); - List tomcatDependencies = new ArrayList(); + List tomcatPluginDependencies = new ArrayList(); ArrayList webapps2Deploy = new ArrayList(); // Add the basic Tomcat dependencies - tomcatDependencies.add( + tomcatPluginDependencies.add( // Packaging goes faster with this lib dependency("org.codehaus.plexus", "plexus-archiver", "2.3")); - tomcatDependencies.add( + tomcatPluginDependencies.add( // The following dependency is needed, otherwise you get // Caused by: java.lang.NoSuchMethodError: // javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig // This method is in Servlet API 3.0 dependency("javax.servlet", "javax.servlet-api", "3.0.1")); + // Do we have any extra Tomcat Plugin dependencies to include? + if (tomcatDependencies != null && tomcatDependencies.size() > 0) { + for (TomcatDependency tomcatDep : tomcatDependencies) { + tomcatPluginDependencies.add( + dependency(tomcatDep.getGroupId(), tomcatDep.getArtifactId(), tomcatDep.getVersion())); + } + } + if (enableH2) { - tomcatDependencies.add( + tomcatPluginDependencies.add( // Bring in the flat file H2 database dependency("com.h2database", "h2", "1.4.190")); // Copy the h2 scripts copyH2Dialect(); + } else if (enableMySQL) { + tomcatPluginDependencies.add( + // Bring in the MySQL JDBC Driver + dependency("mysql", "mysql-connector-java", "5.1.32")); + } else if (enablePostgreSQL) { + tomcatPluginDependencies.add( + // Bring in the PostgreSQL JDBC Driver + dependency("org.postgresql", "postgresql", "9.4-1201-jdbc41")); } if (enablePlatform) { @@ -757,7 +820,7 @@ public class RunMojo extends AbstractMojo { groupId("org.apache.tomcat.maven"), artifactId("tomcat7-maven-plugin"), version(MAVEN_TOMCAT7_PLUGIN_VERSION), - tomcatDependencies + tomcatPluginDependencies ), goal("run"), configuration( diff --git a/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/MavenDependency.java b/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/MavenDependency.java new file mode 100644 index 00000000..53ab3f4b --- /dev/null +++ b/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/MavenDependency.java @@ -0,0 +1,94 @@ +/** + * Copyright (C) 2016 Alfresco Software Limited. + *

+ * This file is part of the Alfresco SDK Samples project. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.alfresco.maven.plugin.config; + +/** + * A Maven Dependency containing the Group ID, Artifact ID, and Version (GAV). + * + * @author martin.bergljung@alfresco.com + * @version 1.0 + * @since 3.0.0 + */ +public abstract class MavenDependency { + private String groupId; + private String artifactId; + private String version; + + public MavenDependency() {} + + public MavenDependency(String g, String a, String v) { + this.groupId = g; + this.artifactId = a; + this.version = v; + } + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getArtifactId() { + return artifactId; + } + + public void setArtifactId(String artifactId) { + this.artifactId = artifactId; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof MavenDependency)) return false; + + MavenDependency that = (MavenDependency) o; + + if (!groupId.equals(that.groupId)) return false; + if (!artifactId.equals(that.artifactId)) return false; + return version.equals(that.version); + + } + + @Override + public int hashCode() { + int result = groupId.hashCode(); + result = 31 * result + artifactId.hashCode(); + result = 31 * result + version.hashCode(); + return result; + } + + @Override + public String toString() { + return "MavenDependency{" + + "groupId='" + groupId + '\'' + + ", artifactId='" + artifactId + '\'' + + ", version='" + version + '\'' + + '}'; + } +} diff --git a/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/ModuleDependency.java b/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/ModuleDependency.java similarity index 61% rename from plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/ModuleDependency.java rename to plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/ModuleDependency.java index 7cc60b73..7e2690e8 100644 --- a/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/ModuleDependency.java +++ b/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/ModuleDependency.java @@ -16,72 +16,59 @@ * limitations under the License. */ -package org.alfresco.maven.plugin; +package org.alfresco.maven.plugin.config; import org.apache.commons.lang.StringUtils; /** * Defines an Alfresco extension module dependency (JAR or AMP) to be - * overlayed on an Alfresco webapp file via Maven WAR plugin. + * overlayed on an Alfresco webapp file. *

* This is so we can skip the WAR projects in the AIO project, * and so we can include the Share Services AMP when running * with a simple platform JAR. *

* This class is used by the RunMojo class. + *

+ * Alfresco Maven Plugin config looks something like this: + *

+ *    {@code
+ *    
+ *      
+ *          ${alfresco.groupId}
+ *          alfresco-share-services
+ *          ${alfresco.share.version}
+ *          amp
+ *      
+ *      
+ *          ${project.groupId}
+ *          ${project.artifactId}
+ *          ${project.version}
+ *      
+ *    
+ *    }
+ * 
* * @author martin.bergljung@alfresco.com * @version 1.0 * @since 3.0.0 */ -public class ModuleDependency { +public class ModuleDependency extends MavenDependency { public static final String TYPE_JAR = "jar"; public static final String TYPE_AMP = "amp"; - private String groupId; - private String artifactId; - private String version; private String type = TYPE_JAR; - public ModuleDependency() {} - - public ModuleDependency(String g, String a, String v) { - this.groupId = g; - this.artifactId = a; - this.version = v; + public ModuleDependency() { + super(); } public ModuleDependency(String g, String a, String v, String t) { - this.groupId = g; - this.artifactId = a; - this.version = v; + super(g,a,v); + this.type = t; } - public String getGroupId() { - return groupId; - } - - public void setGroupId(String groupId) { - this.groupId = groupId; - } - - public String getArtifactId() { - return artifactId; - } - - public void setArtifactId(String artifactId) { - this.artifactId = artifactId; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - public String getType() { return type; } @@ -102,21 +89,16 @@ public class ModuleDependency { public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof ModuleDependency)) return false; + if (!super.equals(o)) return false; ModuleDependency that = (ModuleDependency) o; - if (!groupId.equals(that.groupId)) return false; - if (!artifactId.equals(that.artifactId)) return false; - if (!version.equals(that.version)) return false; return !(type != null ? !type.equals(that.type) : that.type != null); - } @Override public int hashCode() { - int result = groupId.hashCode(); - result = 31 * result + artifactId.hashCode(); - result = 31 * result + version.hashCode(); + int result = super.hashCode(); result = 31 * result + (type != null ? type.hashCode() : 0); return result; } @@ -124,9 +106,9 @@ public class ModuleDependency { @Override public String toString() { return "ModuleDependency{" + - "groupId='" + groupId + '\'' + - ", artifactId='" + artifactId + '\'' + - ", version='" + version + '\'' + + "groupId='" + getGroupId() + '\'' + + ", artifactId='" + getArtifactId() + '\'' + + ", version='" + getVersion() + '\'' + ", type='" + type + '\'' + '}'; } diff --git a/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/TomcatDependency.java b/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/TomcatDependency.java new file mode 100644 index 00000000..0bb7dd17 --- /dev/null +++ b/plugins/alfresco-maven-plugin/src/main/java/org/alfresco/maven/plugin/config/TomcatDependency.java @@ -0,0 +1,61 @@ +/** + * Copyright (C) 2016 Alfresco Software Limited. + *

+ * This file is part of the Alfresco SDK Samples project. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.alfresco.maven.plugin.config; + +import org.apache.commons.lang.StringUtils; + +/** + * Tomcat Dependency used in Embedded Tomcat Maven plugin configuration. + *

+ * Alfresco Maven Plugin config looks something like this: + *

+ *    {@code
+ *    
+ *      
+ *          mysql
+ *          mysql-connector-java
+ *          5.1.32
+ *      
+ *    
+ *    }
+ * 
+ * + * @author martin.bergljung@alfresco.com + * @version 1.0 + * @since 3.0.0 + */ +public class TomcatDependency extends MavenDependency { + + public TomcatDependency() { + super(); + } + + public TomcatDependency(String g, String a, String v) { + super(g,a,v); + } + + @Override + public String toString() { + return "TomcatDependency{" + + "groupId='" + getGroupId() + '\'' + + ", artifactId='" + getArtifactId() + '\'' + + ", version='" + getVersion() + '\'' + + '}'; + } +}