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.slf4j.LoggerFactory;
024 import org.sonar.api.config.Settings;
025 import org.sonar.api.platform.Server;
026 import org.sonar.server.platform.ServerSettings;
027 import org.sonar.server.platform.DefaultServerFileSystem;
028
029 import java.io.File;
030 import java.io.IOException;
031
032 public class MavenRepository {
033
034 private final DefaultServerFileSystem installation;
035 private final String serverId;
036 private File rootDir;
037
038 public MavenRepository(Settings settings, DefaultServerFileSystem fileSystem, Server server) throws IOException {
039 this.installation = fileSystem;
040 this.serverId = server.getId();
041 initRootDir(settings);
042 }
043
044 /**
045 * for unit tests
046 */
047 protected MavenRepository(DefaultServerFileSystem installation, String serverId, File rootDir) {
048 this.installation = installation;
049 this.serverId = serverId;
050 this.rootDir = rootDir;
051 }
052
053 public void start() {
054 try {
055 Artifact maven2Plugin = Mojo.createMaven2Plugin(serverId, installation.getMaven2Plugin());
056 maven2Plugin.deployTo(rootDir);
057
058 } catch (Exception e) {
059 LoggerFactory.getLogger(getClass()).error("Fail to deploy Maven 2 plugin to: " + rootDir, e);
060 throw new IllegalStateException("Fail to deploy Maven 2 plugin to: " + rootDir, e);
061 }
062 }
063
064
065 private void initRootDir(Settings settings) throws IOException {
066 this.rootDir = new File(settings.getString(ServerSettings.DEPLOY_DIR), "maven");
067 File orgDir = new File(rootDir, "/org/");
068 if (orgDir.exists()) {
069 FileUtils.forceDelete(orgDir);
070 }
071 }
072 }