001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.server.mavendeployer; 021 022 import org.apache.commons.io.FileUtils; 023 import org.apache.commons.lang.StringUtils; 024 import org.sonar.api.utils.TempFileUtils; 025 import org.sonar.api.utils.ZipUtils; 026 027 import java.io.File; 028 import java.io.IOException; 029 030 public final class Mojo extends Artifact { 031 032 private Mojo(String artifactId, String version, File jar) { 033 super(BASE_GROUP_ID, artifactId, version, "maven-plugin", jar); 034 } 035 036 public static Mojo createMaven2Plugin(String version, File jar) { 037 return new Mojo("sonar-core-maven-plugin", version, jar); 038 } 039 040 @Override 041 protected String getTemplatePath() { 042 return "/org/sonar/server/mavendeployer/" + getArtifactId() + ".template"; 043 } 044 045 @Override 046 protected void copyTo(File toDir) throws IOException { 047 File tmpDir = TempFileUtils.createTempDirectory("sonar-server-"); 048 try { 049 copyTo(toDir, tmpDir); 050 051 } finally { 052 destroyTmpDir(tmpDir); 053 } 054 } 055 056 protected void copyTo(File toDir, File temporaryDir) throws IOException { 057 ZipUtils.unzip(jar, temporaryDir); 058 059 File metadataFile = new File(temporaryDir, "META-INF/maven/plugin.xml"); 060 String metadata = FileUtils.readFileToString(metadataFile); 061 metadata = updateVersion(metadata, version); 062 063 FileUtils.writeStringToFile(metadataFile, metadata); 064 ZipUtils.zipDir(temporaryDir, new File(toDir, jar.getName())); 065 } 066 067 protected String updateVersion(String metadata, String version) { 068 String before = StringUtils.substringBefore(metadata, "<version>"); 069 String after = StringUtils.substringAfter(metadata, "</version>"); 070 return before + "<version>" + version + "</version>" + after; 071 } 072 073 private void destroyTmpDir(File tmpDir) { 074 if (tmpDir != null) { 075 FileUtils.deleteQuietly(tmpDir); 076 } 077 } 078 }