Author: lcorneliussen Date: Mon Apr 30 17:20:00 2012 New Revision: 1332302 URL: http://svn.apache.org/viewvc?rev=1332302&view=rev Log: [NPANDAY-563] Generic MSDeploy synchronization mojo
o a first working implementation (mvn 3 only) Added: incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncCommand.java (contents, props changed) - copied, changed from r1331359, incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/UnpackDependencyIterationItem.java incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncDestination.java incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncEvent.java incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncMojo.java (contents, props changed) - copied, changed from r1331360, incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/MsDeployResolveWebRolesMojo.java Copied: incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncCommand.java (from r1331359, incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/UnpackDependencyIterationItem.java) URL: http://svn.apache.org/viewvc/incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncCommand.java?p2=incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncCommand.java&p1=incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/UnpackDependencyIterationItem.java&r1=1331359&r2=1332302&rev=1332302&view=diff ============================================================================== --- incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/UnpackDependencyIterationItem.java (original) +++ incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncCommand.java Mon Apr 30 17:20:00 2012 @@ -1,5 +1,3 @@ -package npanday.plugin.msdeploy; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -19,52 +17,178 @@ package npanday.plugin.msdeploy; * under the License. */ -import npanday.PathUtil; +package npanday.plugin.msdeploy; + +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.project.MavenProject; import java.io.File; +import java.util.List; /** * @author <a href="mailto:lcornelius...@apache.org">Lars Corneliussen</a> */ -public class UnpackDependencyIterationItem +public class SyncCommand { - private File packageSource; + private Artifact artifact; - private File packageTarget; + private String groupId; - private Artifact artifact; + private String artifactId; + + private String version; + + private String contentPath; + + private SyncEvent preSync, postSync; - public UnpackDependencyIterationItem( MavenProject project, Artifact artifact ) throws MojoFailureException + private SyncDestination destination; + + public void contextualize( Artifact artifact, SyncDestination destination ) throws MojoFailureException { this.artifact = artifact; - if (!artifact.isResolved()){ - throw new MojoFailureException( "NPANDAY-124-000: The artifact should already have been resolved: " + artifact); + if ( this.destination == null ) + { + this.destination = destination; } - packageSource = artifact.getFile(); - assert packageSource != null : "package source should not be null here"; - - packageTarget = new File( PathUtil.getPreparedPackageFolder( project ), artifact.getArtifactId() ); + if ( !artifact.isResolved() ) + { + throw new MojoFailureException( + "NPANDAY-154-000: The artifact should already have been resolved: " + artifact + ); + } } public File getPackageSource() { - return packageSource; + return artifact.getFile(); } - public File getPackageTarget() - { - return packageTarget; - } @Override public String toString() { - return "UnpackDependencyIterationItem{" + "packageSource=" + packageSource + ", packageTarget=" + packageTarget - + ", artifact=" + artifact + '}'; + return "SyncCommand{" + "packageSource=" + groupId + ":" + artifactId + ":" + version + ", packageTarget=" + + getDestinationArgument() + '}'; + } + + static Joiner JOIN_ON_COMMA = Joiner.on( "," ).skipNulls(); + + public String getDestinationArgument() + { + List<String> parts = Lists.newArrayList(); + + if ( contentPath != null ) + { + parts.add( "contentPath=" + contentPath ); + } + + if (destination != null) { + if ( destination.getComputerName() != null ) + { + parts.add( "computerName=" + destination.getComputerName() ); + } + + if ( destination.getUsername() != null ) + { + parts.add( "username=" + destination.getUsername() ); + } + + if ( destination.getPassword() != null ) + { + parts.add( "password=" + destination.getPassword() ); + } + + if ( destination.getAuthType() != null ) + { + parts.add( "authType=" + destination.getAuthType() ); + } + } + + return JOIN_ON_COMMA.join( parts ); + } + + public Artifact getArtifact() + { + return artifact; + } + + public void setArtifact( Artifact artifact ) + { + this.artifact = artifact; + } + + 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 getContentPath() + { + return contentPath; + } + + public void setContentPath( String contentPath ) + { + this.contentPath = contentPath; + } + + public SyncEvent getPreSync() + { + return preSync; + } + + public void setPreSync( SyncEvent preSync ) + { + this.preSync = preSync; + } + + public SyncEvent getPostSync() + { + return postSync; + } + + public void setPostSync( SyncEvent postSync ) + { + this.postSync = postSync; + } + + public SyncDestination getDestination() + { + return destination; + } + + public void setDestination( SyncDestination destination ) + { + this.destination = destination; } } Propchange: incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncCommand.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncDestination.java URL: http://svn.apache.org/viewvc/incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncDestination.java?rev=1332302&view=auto ============================================================================== --- incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncDestination.java (added) +++ incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncDestination.java Mon Apr 30 17:20:00 2012 @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 npanday.plugin.msdeploy; + +import org.apache.maven.plugin.MojoExecutionException; + +/** + * @author <a href="mailto:m...@lcorneliussen.de>Lars Corneliussen, Faktum Software</a> + */ +public class SyncDestination +{ + private String computerName, username, password, authType, serverId; + + public String getComputerName() + { + return computerName; + } + + public void setComputerName( String computerName ) + { + this.computerName = computerName; + } + + public String getUsername() + { + return username; + } + + public void setUsername( String username ) throws MojoExecutionException + { + throw new MojoExecutionException( "NPANDAY-154-000: Please use settings to store credentials by server id!" ); + } + + public String getPassword() + { + return password; + } + + public void setPassword( String password ) throws MojoExecutionException + { + throw new MojoExecutionException( "NPANDAY-154-002: Please use settings to store credentials by server id!" ); + } + + public String getAuthType() + { + return authType; + } + + public void setAuthType( String authType ) + { + this.authType = authType; + } + + public String getServerId() + { + return serverId; + } + + public void setServerId( String serverId ) + { + this.serverId = serverId; + } + + protected void setSettingsUsername( String username ) + { + this.username = username; + } + + protected void setSettingsPassword( String password ) + { + this.password = password; + } +} Added: incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncEvent.java URL: http://svn.apache.org/viewvc/incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncEvent.java?rev=1332302&view=auto ============================================================================== --- incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncEvent.java (added) +++ incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncEvent.java Mon Apr 30 17:20:00 2012 @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 npanday.plugin.msdeploy; + +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; + +import java.util.List; + +/** + * Configuration class for preSync and postSync commands. + * + * @author <a href="mailto:m...@lcorneliussen.de>Lars Corneliussen, Faktum Software</a> + */ +public class SyncEvent +{ + String runCommand; + int waitInterval; + int waitAttempts; + boolean dontUseCommandExe; + + public String getRunCommand() + { + return runCommand; + } + + public void setRunCommand( String runCommand ) + { + this.runCommand = runCommand; + } + + public int getWaitInterval() + { + return waitInterval; + } + + public void setWaitInterval( int waitInterval ) + { + this.waitInterval = waitInterval; + } + + static Joiner JOIN_ON_COMMA = Joiner.on( "," ).skipNulls(); + + public String getArgumentPart(){ + List<String> parts = Lists.newArrayList(); + + parts.add( "runCommand=\"" + runCommand + "\""); + + if ( waitInterval > 0 ) + { + parts.add( "waitInterval=" + waitInterval ); + } + + if ( waitAttempts > 0 ) + { + parts.add( "waitAttempts=" + waitAttempts ); + } + + if ( dontUseCommandExe ) + { + parts.add( "dontUseCommandExe=true" ); + } + + + return JOIN_ON_COMMA.join( parts ); + } +} Copied: incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncMojo.java (from r1331360, incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/MsDeployResolveWebRolesMojo.java) URL: http://svn.apache.org/viewvc/incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncMojo.java?p2=incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncMojo.java&p1=incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/MsDeployResolveWebRolesMojo.java&r1=1331360&r2=1332302&rev=1332302&view=diff ============================================================================== --- incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/MsDeployResolveWebRolesMojo.java (original) +++ incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncMojo.java Mon Apr 30 17:20:00 2012 @@ -1,5 +1,3 @@ -package npanday.plugin.msdeploy; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -19,69 +17,211 @@ package npanday.plugin.msdeploy; * under the License. */ +package npanday.plugin.msdeploy; + +import com.google.common.base.Strings; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import npanday.ArtifactType; +import npanday.LocalRepositoryUtil; +import npanday.resolver.NPandayArtifactResolver; import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.settings.Server; +import org.apache.maven.settings.Settings; +import org.codehaus.plexus.PlexusConstants; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.codehaus.plexus.context.Context; +import org.codehaus.plexus.context.ContextException; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; +import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.List; import java.util.Set; -import static com.google.common.collect.Lists.newArrayList; - /** - * Resolves all MSDeploy-Packages from project dependencies and unpacks them - * for repackaging through the azure-maven-plugin. + * Resolves all MSDeploy-Packages from project dependencies and syncs + * them using the specified configurations. * * @author <a href="mailto:lcornelius...@apache.org">Lars Corneliussen</a> - * - * @goal resolve-azure-web-roles - * TODO requiresDependencyResolution compile + * @goal sync */ -public class MsDeployResolveWebRolesMojo - extends AbstractMsDeployMojo<UnpackDependencyIterationItem> +public class SyncMojo + extends AbstractMsDeployMojo<SyncCommand> + implements Contextualizable { + /** + * @parameter default-value="false" + */ + boolean allowUntrusted; + + /** + * @parameter default-value="${settings.localRepository}" + */ + String localRepository; + + /** + * @parameter + */ + List<SyncCommand> items; + + /** + * @parameter + */ + SyncDestination destination; + + /** + * @parameter default-value="${settings}" + */ + Settings settings; + + /** + * @component + */ + ArtifactFactory artifactFactory; + + /** + * @component + */ + NPandayArtifactResolver artifactResolver; + + private PlexusContainer container; + + private Object maven2Or3SecDispatcher; + @Override - protected void afterCommandExecution( UnpackDependencyIterationItem iterationItem ) throws MojoExecutionException + public void execute() throws MojoExecutionException, MojoFailureException { + try + { + maven2Or3SecDispatcher = container.lookup( SecDispatcher.ROLE, "maven" ); + } + catch ( ComponentLookupException e ) + { + throw new MojoExecutionException( "NPANDAY-153-003: Error on resolving SecDispatcher", e ); + } + + super.execute(); } @Override - protected void beforeCommandExecution( UnpackDependencyIterationItem iterationItem ) + protected void afterCommandExecution( SyncCommand iterationItem ) throws MojoExecutionException { } @Override - protected List<UnpackDependencyIterationItem> prepareIterationItems() throws MojoFailureException + protected void beforeCommandExecution( SyncCommand iterationItem ) { - List<UnpackDependencyIterationItem> items = newArrayList(); - final Set projectDependencyArtifacts = project.getDependencyArtifacts(); - for ( Object artifactAsObject : projectDependencyArtifacts ) + } + + @Override + protected List<SyncCommand> prepareIterationItems() throws MojoFailureException, MojoExecutionException + { + Set<Artifact> artifacts = Sets.newHashSet(); + + setCredentials( destination ); + + for ( SyncCommand item : items ) { - Artifact artifact = (Artifact)artifactAsObject; - if (artifact.getType().equals( ArtifactType.MSDEPLOY_PACKAGE.getPackagingType()) - || artifact.getType().equals( ArtifactType.MSDEPLOY_PACKAGE.getExtension())) + Artifact artifact = artifactFactory.createDependencyArtifact( + item.getGroupId(), item.getArtifactId(), VersionRange.createFromVersion( item.getVersion() ), + ArtifactType.MSDEPLOY_PACKAGE.getPackagingType(), null, "compile" + ); + artifacts.add( artifact ); + + try { - items.add( new UnpackDependencyIterationItem(project, artifact) ); + artifactResolver.resolve( artifact, project.getRemoteArtifactRepositories(), LocalRepositoryUtil + .create( localRepository ) ); } + catch ( ArtifactResolutionException e ) + { + throw new MojoExecutionException( "NPANDAY-153-001: unable to resolve msdeploy package " + artifact.getId(), e ); + } + catch ( ArtifactNotFoundException e ) + { + throw new MojoExecutionException( "NPANDAY-153-002: unable to resolve msdeploy package " + artifact.getId(), e ); + } + + setCredentials( item.getDestination() ); + + item.contextualize( artifact, destination ); } + return items; } + private void setCredentials( SyncDestination destination ) throws MojoExecutionException + { + if (destination == null) + return; + + if ( !Strings.isNullOrEmpty( destination.getServerId() ) ) + { + Server server = settings.getServer( destination.getServerId() ); + + if (server == null){ + throw new MojoExecutionException( "NPANDAY-153-004: Could not find credentials for server " + destination.getServerId() ); + } + + destination.setSettingsUsername( server.getUsername() ); + destination.setSettingsPassword( decrypt( server.getPassword() ) ); + } + } + + private String decrypt( String password ) throws MojoExecutionException + { + try + { + Method decrypt = maven2Or3SecDispatcher.getClass().getMethod( "decrypt", String.class ); + return (String) decrypt.invoke( + maven2Or3SecDispatcher, password + ); + } + catch ( Exception e ) + { + throw new MojoExecutionException( "NPANDAY-153-005: Error on decrypting password", e ); + } + } + @Override - protected List<String> getCommands(UnpackDependencyIterationItem item) throws MojoExecutionException + protected List<String> getCommands( SyncCommand item ) throws MojoExecutionException, MojoFailureException { List<String> commands = Lists.newArrayList(); commands.add( "-verb:sync" ); - commands.add( "-source:package=" + item.getPackageSource().getAbsolutePath()); - commands.add( "-dest:contentPath=" + item.getPackageTarget().getAbsolutePath() ); + commands.add( "-source:package=" + item.getPackageSource().getAbsolutePath() ); + commands.add( "-dest:" + item.getDestinationArgument() ); + if (item.getPreSync() != null){ + commands.add( "-preSync:" + item.getPreSync().getArgumentPart() ); + } + + if (item.getPostSync() != null){ + commands.add( "-postSync:" + item.getPostSync().getArgumentPart() ); + } + + if ( allowUntrusted ) + { + commands.add( "-allowUntrusted" ); + } return commands; } + + public void contextualize( Context context ) throws ContextException + { + container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); + } } Propchange: incubator/npanday/trunk/plugins/msdeploy-maven-plugin/src/main/java/npanday/plugin/msdeploy/SyncMojo.java ------------------------------------------------------------------------------ svn:eol-style = native