View Javadoc

1   /*
2    * $HeadURL: https://mindtreeinsight.svn.sourceforge.net/svnroot/mindtreeinsight/releng/maven-nsis-plugin/branches/maven-nsis-plugin-0.2.0/src/main/java/com/mindtree/techworks/insight/releng/mvn/nsis/actions/resolver/DependencyResolver.java $
3    * 
4    * Copyright (c) 2008 MindTree Consulting Ltd. 
5    * 
6    * This file is part of Insight Release Engineering Tools.
7    * 
8    * Insight Release Engineering Tools is free software: you can redistribute it 
9    * and/or modify it under the terms of the GNU General Public License as 
10   * published by the Free Software Foundation, either version 3 of the License, 
11   * or (at your option) any later version.
12   * 
13   * Insight Release Engineering Tools is distributed in the hope that it will be 
14   * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
16   * Public License for more details.
17   * 
18   * You should have received a copy of the GNU General Public License along with 
19   * Insight Release Engineering Tools. If not, see <http://www.gnu.org/licenses/>.
20   */
21  package com.mindtree.techworks.insight.releng.mvn.nsis.actions.resolver;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
32  import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
33  import org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter;
34  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.util.FileUtils;
37  
38  import com.mindtree.techworks.insight.releng.mvn.nsis.actions.MojoInfo;
39  import com.mindtree.techworks.insight.releng.mvn.nsis.actions.NsisActionExecutionException;
40  import com.mindtree.techworks.insight.releng.mvn.nsis.model.DependencySet;
41  import com.mindtree.techworks.insight.releng.mvn.nsis.model.SetBase;
42  
43  /**
44   * Resolves the dependencies
45   *
46   * @author <a href="mailto:bindul_bhowmik@mindtree.com">Bindul Bhowmik</a>
47   * @version $Revision: 97 $ $Date: 2008-01-08 00:47:32 -0700 (Tue, 08 Jan 2008) $
48   *
49   * @plexus.component role="com.mindtree.techworks.insight.releng.mvn.nsis.actions.resolver.Resolver" role-hint="dependency"
50   */
51  public class DependencyResolver implements Resolver {
52  
53  	/* (non-Javadoc)
54  	 * @see com.mindtree.techworks.insight.releng.mvn.nsis.actions.resolver.Resolver#copyFiles(com.mindtree.techworks.insight.releng.mvn.nsis.model.SetBase, com.mindtree.techworks.insight.releng.mvn.nsis.actions.MojoInfo, java.io.File)
55  	 */
56  	public void copyFiles(SetBase setBase, MojoInfo mojoInfo,
57  			File archiveTempDir) throws NsisActionExecutionException {
58  		DependencySet dependencySet = (DependencySet) setBase;
59  		
60  		File[] selectedFiles = resolveDependencies(dependencySet, mojoInfo);
61  		
62  		File destinationDir = new File(archiveTempDir, ((null == dependencySet
63  				.getOutputDirectory()) ? "" : dependencySet.getOutputDirectory()));
64  		
65  		if (!destinationDir.exists()) {
66  			if (!destinationDir.mkdirs()) {
67  				throw new NsisActionExecutionException("Could not create " +
68  						"destination directory: " + destinationDir.getAbsolutePath());
69  			}
70  		}
71  		
72  		for (int i = 0; i < selectedFiles.length; i++) {
73  			File destinationFile = new File(destinationDir, selectedFiles[i].getName());
74  			mojoInfo.getLog().debug(
75  					"Copying: " + selectedFiles[i] + " to "
76  							+ destinationFile.getAbsolutePath());
77  			try {
78  				FileUtils.copyFile(selectedFiles[i], destinationFile);
79  			} catch (IOException e) {
80  				mojoInfo.getLog().error("Error copying " + selectedFiles[i], e);
81  				throw new NsisActionExecutionException("Error copying "
82  						+ selectedFiles[i], e);
83  			}
84  		}
85  	}
86  
87  	/* (non-Javadoc)
88  	 * @see com.mindtree.techworks.insight.releng.mvn.nsis.actions.resolver.Resolver#getFileNames(com.mindtree.techworks.insight.releng.mvn.nsis.model.SetBase, com.mindtree.techworks.insight.releng.mvn.nsis.actions.MojoInfo)
89  	 */
90  	public List getRelativeFilePath(SetBase setBase, MojoInfo mojoInfo)
91  			throws NsisActionExecutionException {
92  		File[] selectedFiles = resolveDependencies((DependencySet) setBase, mojoInfo);
93  		
94  		List relativePaths = new ArrayList(selectedFiles.length);
95  		String relativeBase = (null == setBase.getOutputDirectory()) ? "" : setBase.getOutputDirectory() + File.separator;
96  		
97  		for (int i = 0; i < selectedFiles.length; i++) {
98  			mojoInfo.getLog().debug("Adding: " + selectedFiles[i].getAbsolutePath());
99  			relativePaths.add(relativeBase + selectedFiles[i].getName());
100 		}
101 		
102 		return relativePaths;
103 	}
104 	
105 	/**
106 	 * Resolves the dependencies
107 	 * @return 
108 	 */
109 	protected File[] resolveDependencies (DependencySet dependencySet, MojoInfo mojoInfo) {
110 		
111 		MavenProject project = mojoInfo.getProject();
112 		Set artifactSet = project.getArtifacts();
113 		
114 		ArtifactFilter scopeArtifactFilter = new ScopeArtifactFilter(dependencySet.getScope());
115 		ArtifactFilter includeArtifactFilter = null;
116 		ArtifactFilter excludeArtifactFilter = null;
117 		if (null != dependencySet.getIncludes()) {
118 			includeArtifactFilter = new IncludesArtifactFilter(dependencySet.getIncludes());
119 		}
120 		if (null != dependencySet.getExcludes()) {
121 			excludeArtifactFilter = new ExcludesArtifactFilter(dependencySet.getExcludes());
122 		}
123 		
124 		List dependentArtifacts = new ArrayList(artifactSet.size());
125 		
126 		for (Iterator artifacts = artifactSet.iterator(); artifacts.hasNext(); ) {
127 			Artifact artifact = (Artifact) artifacts.next();
128 			if (includeArtifact (artifact, scopeArtifactFilter, includeArtifactFilter, excludeArtifactFilter)) {
129 				mojoInfo.getLog().debug("Including dependency: " + artifact.getId());
130 				
131 				File outputFile = artifact.getFile();
132 				
133 				mojoInfo.getLog().debug("Inluding dependency as: " + outputFile.getAbsolutePath());
134 				dependentArtifacts.add(outputFile);
135 			}
136 		}
137 		
138 		return (File[]) dependentArtifacts.toArray(new File[dependentArtifacts.size()]);
139 	}
140 	
141 	/**
142 	 * Checks if an artifact has to be included
143 	 */
144 	private boolean includeArtifact(Artifact artifact,
145 			ArtifactFilter scopeArtifactFilter,
146 			ArtifactFilter includeArtifactFilter,
147 			ArtifactFilter excludeArtifactFilter) {
148 
149 		boolean include = false;
150 		include = scopeArtifactFilter.include(artifact);
151 		
152 		if (null != includeArtifactFilter) {
153 			include = includeArtifactFilter.include(artifact);
154 		}
155 		
156 		if (null != excludeArtifactFilter) {
157 			include = excludeArtifactFilter.include(artifact);
158 		}
159 		
160 		return include;
161 	}
162 }