| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.codehaus.mojo.nbm; |
| 18 | |
|
| 19 | |
import java.io.File; |
| 20 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 21 | |
import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 22 | |
import org.apache.maven.plugins.annotations.Mojo; |
| 23 | |
import org.apache.maven.plugins.annotations.Parameter; |
| 24 | |
import org.apache.maven.project.MavenProject; |
| 25 | |
import org.codehaus.plexus.archiver.jar.JarArchiver; |
| 26 | |
import org.codehaus.plexus.util.DirectoryScanner; |
| 27 | |
import org.codehaus.plexus.util.FileUtils; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
@Mojo(name="branding", |
| 53 | |
requiresProject=true, |
| 54 | |
threadSafe = true, |
| 55 | |
defaultPhase= LifecyclePhase.PACKAGE) |
| 56 | 0 | public class BrandingMojo |
| 57 | |
extends AbstractNbmMojo |
| 58 | |
{ |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
@Parameter(required=true, defaultValue="${project.build.directory}/nbm") |
| 64 | |
protected File nbmBuildDir; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
@Parameter(defaultValue="${project.build.directory}", required=true) |
| 70 | |
protected File outputDirectory; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
@Parameter(required=true, defaultValue="${basedir}/src/main/nbm-branding") |
| 76 | |
private File brandingSources; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
@Parameter(property="netbeans.branding.token") |
| 82 | |
private String brandingToken; |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
@Parameter(required=true, defaultValue="extra") |
| 87 | |
protected String cluster; |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
@Parameter(required=true, readonly=true, property="project") |
| 94 | |
private MavenProject project; |
| 95 | |
|
| 96 | |
public void execute() |
| 97 | |
throws MojoExecutionException |
| 98 | |
{ |
| 99 | 0 | if ( !"nbm".equals( project.getPackaging() ) ) |
| 100 | |
{ |
| 101 | 0 | getLog().error( "The nbm:branding goal shall be used within a NetBeans module project only (packaging 'nbm')" ); |
| 102 | |
} |
| 103 | 0 | if ( !brandingSources.isDirectory() ) |
| 104 | |
{ |
| 105 | 0 | getLog().info( "No branding to process." ); |
| 106 | 0 | return; |
| 107 | |
} |
| 108 | 0 | if ( brandingToken == null ) |
| 109 | |
{ |
| 110 | 0 | throw new MojoExecutionException( "brandingToken must be defined for mojo:branding" ); |
| 111 | |
} |
| 112 | |
try |
| 113 | |
{ |
| 114 | |
|
| 115 | 0 | DirectoryScanner scanner = new DirectoryScanner(); |
| 116 | 0 | scanner.setIncludes( new String[] |
| 117 | |
{ |
| 118 | |
"**/*.*" |
| 119 | |
} ); |
| 120 | 0 | scanner.addDefaultExcludes(); |
| 121 | 0 | scanner.setBasedir( brandingSources ); |
| 122 | 0 | scanner.scan(); |
| 123 | |
|
| 124 | 0 | final String clusterPathPart = "netbeans" + File.separator + cluster; |
| 125 | 0 | File outputDir = new File(outputDirectory, "branding_and_locales"); |
| 126 | 0 | outputDir.mkdirs(); |
| 127 | 0 | File clusterDir = new File( nbmBuildDir, clusterPathPart ); |
| 128 | 0 | clusterDir.mkdirs(); |
| 129 | |
|
| 130 | |
|
| 131 | 0 | for ( String brandingFilePath : scanner.getIncludedFiles() ) |
| 132 | |
{ |
| 133 | 0 | File brandingFile = new File( brandingSources, brandingFilePath ); |
| 134 | 0 | String[] locale = getLocale( brandingFile.getName()); |
| 135 | 0 | String token = locale[1] == null ? brandingToken : brandingToken + "_" + locale[1]; |
| 136 | 0 | File root = new File(outputDir, token); |
| 137 | 0 | root.mkdirs(); |
| 138 | 0 | String destinationName = locale[0] + "_" + token + locale[2]; |
| 139 | 0 | File brandingDestination = new File( root, brandingFilePath.replace( brandingFile.getName(), destinationName) ); |
| 140 | 0 | if ( !brandingDestination.getParentFile().exists() ) |
| 141 | |
{ |
| 142 | 0 | brandingDestination.getParentFile().mkdirs(); |
| 143 | |
} |
| 144 | 0 | FileUtils.copyFile( brandingFile, brandingDestination ); |
| 145 | |
} |
| 146 | 0 | for (File rootDir : outputDir.listFiles()) { |
| 147 | 0 | if (!rootDir.isDirectory()) { |
| 148 | 0 | continue; |
| 149 | |
} |
| 150 | 0 | String effectiveBranding = rootDir.getName(); |
| 151 | |
|
| 152 | 0 | scanner.setIncludes( new String[] |
| 153 | |
{ |
| 154 | |
"**/*.jar" |
| 155 | |
} ); |
| 156 | 0 | scanner.setBasedir( rootDir ); |
| 157 | 0 | scanner.scan(); |
| 158 | 0 | for ( String jarDirectoryPath : scanner.getIncludedDirectories() ) |
| 159 | |
{ |
| 160 | |
|
| 161 | 0 | File jarDirectory = new File( rootDir, jarDirectoryPath ); |
| 162 | 0 | File destinationLocation = new File(clusterDir, jarDirectoryPath).getParentFile(); |
| 163 | 0 | destinationLocation.mkdirs(); |
| 164 | |
|
| 165 | 0 | File destinationJar = |
| 166 | |
new File( destinationLocation + File.separator + "locale" |
| 167 | |
+ File.separator + destinationFileName( jarDirectory.getName(), effectiveBranding ) ); |
| 168 | |
|
| 169 | |
|
| 170 | 0 | JarArchiver archiver = new JarArchiver(); |
| 171 | 0 | archiver.setDestFile( destinationJar ); |
| 172 | 0 | archiver.addDirectory( jarDirectory ); |
| 173 | 0 | archiver.createArchive(); |
| 174 | |
} |
| 175 | |
} |
| 176 | |
|
| 177 | |
} |
| 178 | 0 | catch ( Exception ex ) |
| 179 | |
{ |
| 180 | 0 | throw new MojoExecutionException( "Error creating branding", ex ); |
| 181 | 0 | } |
| 182 | 0 | } |
| 183 | |
|
| 184 | |
static String destinationFileName( String brandingFilePath, String branding ) |
| 185 | |
{ |
| 186 | |
|
| 187 | 0 | int lastSeparator = brandingFilePath.lastIndexOf( File.separator ); |
| 188 | 0 | String infix = "_" + branding; |
| 189 | |
|
| 190 | |
|
| 191 | 0 | int lastDot = brandingFilePath.lastIndexOf( "." ); |
| 192 | 0 | if (lastDot == -1 || lastDot < lastSeparator) { |
| 193 | 0 | return brandingFilePath + infix; |
| 194 | |
} |
| 195 | 0 | return brandingFilePath.substring( 0, lastDot ) + infix + brandingFilePath.substring( lastDot ); |
| 196 | |
} |
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
static String[] getLocale(String name) { |
| 202 | 0 | String suffix = ""; |
| 203 | 0 | int dot = name.indexOf( "."); |
| 204 | 0 | if (dot > -1) { |
| 205 | 0 | suffix = name.substring( dot ); |
| 206 | 0 | name = name.substring( 0, dot); |
| 207 | |
} |
| 208 | 0 | String locale = null; |
| 209 | 0 | int count = 1; |
| 210 | |
|
| 211 | 0 | while (count <= 3) { |
| 212 | 0 | int underscore = name.lastIndexOf( '_'); |
| 213 | 0 | if (underscore > -1) { |
| 214 | 0 | String loc1 = name.substring( underscore + 1); |
| 215 | 0 | if (loc1.length() != 2) { |
| 216 | 0 | break; |
| 217 | |
} |
| 218 | 0 | locale = loc1 + (locale == null ? "" : "_" + locale); |
| 219 | 0 | name = name.substring( 0, underscore); |
| 220 | |
} else { |
| 221 | |
break; |
| 222 | |
} |
| 223 | 0 | count = count + 1; |
| 224 | 0 | } |
| 225 | 0 | return new String[] {name, locale, suffix}; |
| 226 | |
} |
| 227 | |
} |