Refactor Refresh Mojo's to be more dynamic and aware of its sourroundings (#283)

This commit is contained in:
Ole Hejlskov
2015-05-11 14:11:38 +02:00
parent 1a95fab1eb
commit 96efddd7af
6 changed files with 157 additions and 110 deletions

View File

@@ -21,7 +21,7 @@
<properties> <properties>
<!-- Defines the target WAR artifactId to run this amp, only used with the -Pamp-to-war switch <!-- Defines the target WAR artifactId to run this amp, only used with the -Pamp-to-war switch
Allowed values: alfresco | share. In this case it's configured to use OOTB share --> Allowed values: alfresco | share. In this case it's configured to use OOTB share -->
<alfresco.client.war>share</alfresco.client.war> <app.amp.client.war.artifactId>share</app.amp.client.war.artifactId>
<!-- Used in share-config-custom.xml. By default points to standard location of Alfresco Repository --> <!-- Used in share-config-custom.xml. By default points to standard location of Alfresco Repository -->
<alfresco.repo.url>http://localhost:8080/alfresco</alfresco.repo.url> <alfresco.repo.url>http://localhost:8080/alfresco</alfresco.repo.url>

View File

@@ -37,7 +37,9 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import java.io.Closeable; import java.io.Closeable;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@@ -53,49 +55,77 @@ import java.util.List;
* @since 2.1.0 * @since 2.1.0
*/ */
public abstract class AbstractRefreshWebappMojo extends AbstractMojo { public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
public static final String DEFAULT_USERNAME = "admin";
public static final String DEFAULT_PASSWORD = "admin"; @Component
public static final String DEFAULT_HOST = "localhost"; protected MavenProject project;
public static final String DEFAULT_PORT = "8080";
/**
* The mode for the refresh goal, current supported values are:
* auto - Checks packaging and app.amp.client.war.artifactId to determine if it should refresh for repo or share
* both - Forces it to refresh both for Repo and Share
* share - Forces only to refresh share
* repo - Forces only to refresh repo
* none - Disables refreshing web scripts
*/
@Parameter(property = "maven.alfresco.refreshMode", defaultValue = "auto")
protected String refreshMode;
/** /**
* The hostname for where the Alfresco Tomcat server is running. * The hostname for where the Alfresco Tomcat server is running.
*/ */
@Parameter(property = "refreshHost", defaultValue = DEFAULT_HOST, alias = "refreshHost") @Parameter(property = "maven.alfresco.refreshHost", defaultValue = "localhost")
private String _host = DEFAULT_HOST; protected String refreshHost;
/** /**
* The port number for where the Alfresco Tomcat server is running. * The port number for where the Alfresco Tomcat server is running.
*/ */
@Parameter(property = "refreshPort", defaultValue = DEFAULT_PORT, alias = "refreshPort") @Parameter(property = "maven.alfresco.refreshPort", defaultValue = "${maven.tomcat.port}")
private String _port = DEFAULT_PORT; private String refreshPort;
/** /**
* The username for authenticating against Alfresco Repo. * The username for authenticating against Alfresco Repo.
*/ */
@Parameter(property = "refreshUsername", defaultValue = DEFAULT_USERNAME, alias = "refreshUsername") @Parameter(property = "maven.alfresco.refreshUsername", defaultValue = "admin")
private String _username = DEFAULT_USERNAME; private String refreshUsername;
/** /**
* The password for authenticating against Alfresco Repo. * The password for authenticating against Alfresco Repo.
*/ */
@Parameter(property = "refreshPassword", defaultValue = DEFAULT_PASSWORD, alias = "refreshPassword") @Parameter(property = "maven.alfresco.refreshPassword", defaultValue = "admin")
private String _password = DEFAULT_PASSWORD; protected String refreshPassword;
/** /**
* The Refresh URL to call, can be either for Share or Repo * The URL to send the POST to when you want to refresh Alfresco Repo Web Scripts container.
*/ */
private String refreshUrl; @Parameter(property = "maven.alfresco.refreshRepoUrl", defaultValue = "/alfresco/service/index")
protected String refreshRepoUrl;
/**
* The URL to send the POST to when you want to refresh Alfresco Share Spring Surf Web Scripts container.
*/
@Parameter(property = "maven.alfresco.refreshShareUrl", defaultValue = "/share/page/index")
protected String refreshShareUrl;
/**
* The URL to send the POST to when you want to clear dependency caches for the Alfresco Share webapp.
*/
@Parameter(property = "maven.alfresco.clearCacheShareUrl", defaultValue = "/share/page/caches/dependency/clear")
protected String clearCacheShareUrl;
/** /**
* The Clear Dependency Caches URL to call, currently only applicable to Share. * The Clear Dependency Caches URL to call, currently only applicable to Share.
*/ */
private String clearDependencyCachesUrl; protected String clearDependencyCachesUrl;
/** /**
* The name of the web application we are refreshing, just for logging purpose * The name of the web application we are refreshing, just for logging purpose
*/ */
private String refreshWebappName; protected String refreshWebappName;
@Parameter(defaultValue = "${app.amp.client.war.artifactId}")
protected String alfrescoClientWar;
/** /**
* Mojo interface implementation * Mojo interface implementation
@@ -105,41 +135,39 @@ public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
public void execute() throws MojoExecutionException { public void execute() throws MojoExecutionException {
// Do a ping to see if the server is up, if not, log and just exit // Do a ping to see if the server is up, if not, log and just exit
if (!ping()) { if (!ping()) {
getLog().warn("Connection failed to " + _host + ":" + _port + ", " + getAbortedMsg()); getLog().warn("Connection failed to " + refreshHost + ":" + refreshPort + ", " + getAbortedMsg());
return; return;
} }
executeRefresh(); executeRefresh();
} }
/** /**
* To be implemented by webapp "refresh" Mojos * To be implemented by webapp "refresh" Mojos
*/ */
protected abstract void executeRefresh(); protected abstract void executeRefresh();
/**
* The following methods are called by specific refresh mojo implementations
*/
protected void setRefreshUrl(String refreshUrl) { protected void _refreshRepo() {
this.refreshUrl = refreshUrl; refreshWebappName = "Alfresco Repository";
refreshWebScripts(refreshRepoUrl);
} }
protected void setClearDependencyCachesUrl(String clearDependencyCachesUrl) { protected void _refreshShare() {
this.clearDependencyCachesUrl = clearDependencyCachesUrl; refreshWebappName = "Alfresco Share";
} refreshWebScripts(refreshShareUrl);
clearDependencyCaches(clearCacheShareUrl);
protected void setWebappName(String refreshWebappName) {
this.refreshWebappName = refreshWebappName;
} }
/** /**
* Perform a Refresh of Web Scripts container in webapp. * Perform a Refresh of Web Scripts container in webapp.
* Called by specific refresh mojo implementation. * Called by specific refresh mojo implementation.
*/ */
protected void refreshWebScripts() { protected void refreshWebScripts(String url) {
// Create the Refresh URL for the Alfresco Tomcat server // Create the Refresh URL for the Alfresco Tomcat server
URL alfrescoTomcatUrl = buildFinalUrl(refreshUrl); URL alfrescoTomcatUrl = buildFinalUrl(url);
if (alfrescoTomcatUrl == null) { if (alfrescoTomcatUrl == null) {
getLog().error("Could not build refresh URL for " + refreshWebappName + ", " + getAbortedMsg()); getLog().error("Could not build refresh URL for " + refreshWebappName + ", " + getAbortedMsg());
} }
@@ -157,9 +185,9 @@ public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
* Perform a Clear Dependency Caches call on Share webapp. * Perform a Clear Dependency Caches call on Share webapp.
* Called by specific refresh mojo implementation, currently only applicable to Share webapp. * Called by specific refresh mojo implementation, currently only applicable to Share webapp.
*/ */
protected void clearDependencyCaches() { protected void clearDependencyCaches(String url) {
// Create the Clear Cache URL for the Alfresco Tomcat server // Create the Clear Cache URL for the Alfresco Tomcat server
URL alfrescoTomcatUrl = buildFinalUrl(clearDependencyCachesUrl); URL alfrescoTomcatUrl = buildFinalUrl(url);
if (alfrescoTomcatUrl == null) { if (alfrescoTomcatUrl == null) {
getLog().error("Could not build clear dependency caches URL for " + getLog().error("Could not build clear dependency caches URL for " +
refreshWebappName + ", " + getAbortedMsg()); refreshWebappName + ", " + getAbortedMsg());
@@ -187,7 +215,7 @@ public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
// Set up authentication parameters // Set up authentication parameters
CredentialsProvider credsProvider = new BasicCredentialsProvider(); CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials(_username, _password)); new UsernamePasswordCredentials(refreshUsername, refreshPassword));
// Create the HTTP Client we will use to make the call // Create the HTTP Client we will use to make the call
client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
@@ -230,7 +258,7 @@ public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
statusCode + ", message: " + reasonPhrase); statusCode + ", message: " + reasonPhrase);
} }
} catch (Exception ex) { } catch (Exception ex) {
getLog().error("POST request failed to " + _host + ":" + _port + refreshUrl + ", " + getAbortedMsg()); getLog().error("POST request failed to " + alfrescoTomcatUrl.toString() + ", " + getAbortedMsg());
getLog().error("Exception Msg: " + ex.getMessage()); getLog().error("Exception Msg: " + ex.getMessage());
} finally { } finally {
closeQuietly(response); closeQuietly(response);
@@ -240,7 +268,7 @@ public abstract class AbstractRefreshWebappMojo extends AbstractMojo {
private URL buildFinalUrl(String specificRefreshUrlPath) { private URL buildFinalUrl(String specificRefreshUrlPath) {
try { try {
return new URL("http://" + _host + ":" + _port + specificRefreshUrlPath); return new URL("http://" + refreshHost + ":" + refreshPort + specificRefreshUrlPath);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;

View File

@@ -0,0 +1,85 @@
/**
* Copyright (C) 2015 Alfresco Software Limited.
* <p/>
* This file is part of the Alfresco SDK.
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Refresh Alfresco Repo and Share Mojo.
* Will refresh the Web Script container so new and changed
* Spring Surf Web Scripts are detected.
* Will also clear the dependency caches for web resources (CSS, JS, etc).
* <p/>
* It is important to execute the refresh calls in the compile phase,
* otherwise the files will be copied after this and the refresh calls
* will not be recognized.
*
* @author martin.bergljung@alfresco.com
* @since 2.1.0
*/
@Mojo(name = "refresh", threadSafe = true, defaultPhase = LifecyclePhase.PROCESS_RESOURCES, aggregator = true)
public class RefreshMojo extends AbstractRefreshWebappMojo {
/**
* Call the Share Webapp and refresh web scripts and clear caches.
*/
protected void executeRefresh() {
switch (refreshMode) {
case "auto":
_autoRefresh();
break;
case "both":
_refreshRepo();
_refreshShare();
break;
case "share":
_refreshShare();
break;
case "repo":
_refreshRepo();
break;
default:
break;
}
}
private void _autoRefresh() {
if ( ! this.project.getPackaging().equalsIgnoreCase("amp") ) {
_refreshRepo();
_refreshShare();
return;
}
if ( this.alfrescoClientWar.startsWith("alfresco") ) {
_refreshRepo();
} else {
_refreshShare();
}
}
}

View File

@@ -19,7 +19,6 @@ package org.alfresco.maven.plugin;
import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/** /**
* Refresh Alfresco Repository (alfresco.war) Mojo. * Refresh Alfresco Repository (alfresco.war) Mojo.
@@ -33,23 +32,11 @@ import org.apache.maven.plugins.annotations.Parameter;
* @author martin.bergljung@alfresco.com * @author martin.bergljung@alfresco.com
* @since 2.1.0 * @since 2.1.0
*/ */
@Mojo(name = "refresh-repo", defaultPhase = LifecyclePhase.COMPILE) @Mojo(name = "refresh-repo", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, requiresProject = true, threadSafe = true)
public class RefreshRepoWebappMojo extends AbstractRefreshWebappMojo { public class RefreshRepoWebappMojo extends AbstractRefreshWebappMojo {
public static final String DEFAULT_REPO_REFRESH_URL = "/alfresco/service/index";
/**
* The URL to send the POST to when you want to refresh Alfresco Repo Web Scripts container.
*/
@Parameter(property = "refreshRepoUrl", required = true, alias = "refreshRepoUrl")
private String _refreshRepoUrl = DEFAULT_REPO_REFRESH_URL;
public RefreshRepoWebappMojo() {
setRefreshUrl(_refreshRepoUrl);
setWebappName("Alfresco Repository");
}
@Override @Override
protected void executeRefresh() { protected void executeRefresh() {
refreshWebScripts(); _refreshRepo();
} }
} }

View File

@@ -19,7 +19,6 @@ package org.alfresco.maven.plugin;
import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/** /**
* Refresh Alfresco Share (share.war) Mojo. * Refresh Alfresco Share (share.war) Mojo.
@@ -34,34 +33,13 @@ import org.apache.maven.plugins.annotations.Parameter;
* @author martin.bergljung@alfresco.com * @author martin.bergljung@alfresco.com
* @since 2.1.0 * @since 2.1.0
*/ */
@Mojo(name = "refresh-share", defaultPhase = LifecyclePhase.COMPILE) @Mojo(name = "refresh-share", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, threadSafe = true, requiresProject = true)
public class RefreshShareWebappMojo extends AbstractRefreshWebappMojo { public class RefreshShareWebappMojo extends AbstractRefreshWebappMojo {
public static final String DEFAULT_SHARE_REFRESH_URL = "/share/page/index";
public static final String DEFAULT_SHARE_CLEAR_DEPENDENCY_CACHES_URL = "/share/page/caches/dependency/clear";
/**
* The URL to send the POST to when you want to refresh Alfresco Share Spring Surf Web Scripts container.
*/
@Parameter(property = "refreshShareUrl", required = true, alias = "refreshShareUrl")
private String _refreshShareUrl = DEFAULT_SHARE_REFRESH_URL;
/**
* The URL to send the POST to when you want to clear dependency caches for the Alfresco Share webapp.
*/
@Parameter(property = "clearCacheShareUrl", required = true, alias = "clearCacheShareUrl")
private String _clearCacheShareUrl = DEFAULT_SHARE_CLEAR_DEPENDENCY_CACHES_URL;
public RefreshShareWebappMojo() {
setRefreshUrl(_refreshShareUrl);
setClearDependencyCachesUrl(_clearCacheShareUrl);
setWebappName("Alfresco Share");
}
/** /**
* Call the Share Webapp and refresh web scripts and clear caches. * Call the Share Webapp and refresh web scripts and clear caches.
*/ */
protected void executeRefresh() { protected void executeRefresh() {
refreshWebScripts(); _refreshShare();
clearDependencyCaches();
} }
} }

View File

@@ -227,13 +227,6 @@
<extensions>true</extensions> <extensions>true</extensions>
<configuration> <configuration>
<snapshotToTimestamp>true</snapshotToTimestamp> <snapshotToTimestamp>true</snapshotToTimestamp>
<refreshHost>localhost</refreshHost>
<refreshPort>${maven.tomcat.port}</refreshPort>
<refreshRepoUrl>/alfresco/service/index</refreshRepoUrl>
<refreshShareUrl>/share/page/index</refreshShareUrl>
<clearCacheShareUrl>/share/page/caches/dependency/clear</clearCacheShareUrl>
<refreshUsername>admin</refreshUsername>
<refreshPassword>admin</refreshPassword>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
@@ -382,6 +375,7 @@
<goal>set-version</goal> <goal>set-version</goal>
<goal>refresh-share</goal> <goal>refresh-share</goal>
<goal>refresh-repo</goal> <goal>refresh-repo</goal>
<goal>refresh</goal>
</goals> </goals>
</pluginExecutionFilter> </pluginExecutionFilter>
<action> <action>
@@ -882,7 +876,7 @@
<profile> <profile>
<id>refresh-repo</id> <id>refresh-both</id>
<activation> <activation>
<file> <file>
<exists>src/main/amp/module.properties</exists> <exists>src/main/amp/module.properties</exists>
@@ -895,10 +889,10 @@
<artifactId>alfresco-maven-plugin</artifactId> <artifactId>alfresco-maven-plugin</artifactId>
<executions> <executions>
<execution> <execution>
<id>refresh-repo</id> <id>refresh-webscripts-repo-and-share</id>
<phase>process-resources</phase> <phase>process-resources</phase>
<goals> <goals>
<goal>refresh-repo</goal> <goal>refresh</goal>
</goals> </goals>
</execution> </execution>
</executions> </executions>
@@ -908,31 +902,6 @@
</profile> </profile>
<profile>
<id>refresh-share</id>
<activation>
<file>
<exists>src/main/amp/module.properties</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.alfresco.maven.plugin</groupId>
<artifactId>alfresco-maven-plugin</artifactId>
<executions>
<execution>
<id>refresh-share</id>
<phase>process-resources</phase>
<goals>
<goal>refresh-share</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- ==================================================================================================== <!-- ====================================================================================================