001 /* 002 * Copyright (C) 2006-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.codehaus.gmaven.plugin; 018 019 import org.apache.maven.shared.io.scan.StaleResourceScanner; 020 import org.apache.maven.shared.io.scan.mapping.SourceMapping; 021 import org.apache.maven.shared.model.fileset.FileSet; 022 import org.codehaus.gmaven.common.ArtifactItem; 023 024 import java.io.File; 025 import java.io.IOException; 026 import java.util.Collections; 027 import java.util.HashSet; 028 import java.util.List; 029 import java.util.Set; 030 031 /** 032 * Support for compile mojos (class and stub gen). 033 * 034 * @version $Id: CompilerMojoSupport.java 8 2009-07-16 09:15:04Z user57 $ 035 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a> 036 */ 037 public abstract class CompilerMojoSupport 038 extends ComponentMojoSupport 039 { 040 /** 041 * Additional artifacts to add to the classpath (in addition to the classpath 042 * which is picked up from the executing poms configuration). 043 * 044 * @parameter 045 */ 046 protected ArtifactItem[] classpath; 047 048 /** 049 * Source files to be included. If not specified, then the default will be used. 050 * 051 * @parameter 052 */ 053 protected FileSet[] sources; 054 055 /** 056 * @component 057 * 058 * @noinspection UnusedDeclaration 059 */ 060 protected CompileState compileState; 061 062 protected CompilerMojoSupport(final String key) { 063 super(key); 064 } 065 066 // 067 // Support 068 // 069 070 protected abstract File getOutputDirectory() throws Exception; 071 072 protected abstract List getSourceRoots(); 073 074 protected void addSourceRoot(final File dir) throws IOException { 075 assert dir != null; 076 077 List roots = getSourceRoots(); 078 assert roots != null; 079 080 String path = dir.getCanonicalPath(); 081 082 if (!roots.contains(path)) { 083 log.debug("Adding source root: {}", path); 084 085 roots.add(path); 086 } 087 } 088 089 protected abstract FileSet[] getDefaultSources(); 090 091 protected ArtifactItem[] getUserClassspathElements() { 092 return classpath; 093 } 094 095 // 096 // File Scanning 097 // 098 099 protected Set getIncludesFrom(final FileSet fileSet) { 100 assert fileSet != null; 101 102 List list = fileSet.getIncludes(); 103 104 if (list != null) { 105 return new HashSet(list); 106 } 107 108 return Collections.singleton("**/*"); 109 } 110 111 protected Set getExcludesFrom(final FileSet fileSet) { 112 assert fileSet != null; 113 114 List list = fileSet.getExcludes(); 115 116 if (list != null) { 117 return new HashSet(list); 118 } 119 120 return Collections.EMPTY_SET; 121 } 122 123 protected File[] scanForSources(final FileSet fileSet, final SourceMapping[] mappings) throws Exception { 124 assert fileSet != null; 125 126 File sourceDir = new File(fileSet.getDirectory()); 127 log.debug("Scanning for sources in: {}", sourceDir); 128 129 if (!sourceDir.exists()) { 130 log.debug("Skipping; missing source directory: {}", sourceDir); 131 132 return new File[0]; 133 } 134 135 File targetDir = getOutputDirectory(); 136 Set includes = getIncludesFrom(fileSet); 137 Set excludes = getExcludesFrom(fileSet); 138 139 StaleResourceScanner scanner = new StaleResourceScanner(0, includes, excludes); 140 141 if (mappings != null && mappings.length > 0) { 142 for (int i=0; i<mappings.length; i++) { 143 scanner.addSourceMapping(mappings[i]); 144 } 145 } 146 147 Set files = scanner.getIncludedSources(sourceDir, targetDir); 148 149 return (File[]) files.toArray(new File[files.size()]); 150 } 151 152 protected File[] scanForSources(final FileSet fileSet, final SourceMapping mapping) throws Exception { 153 assert mapping != null; 154 155 return scanForSources(fileSet, new SourceMapping[] { mapping }); 156 } 157 }