| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CheckclipseConfigSettingsMojo |
|
| 9.0;9 |
| 1 | /* | |
| 2 | * EL4J, the Extension Library for the J2EE, adds incremental enhancements to | |
| 3 | * the spring framework, http://el4j.sf.net | |
| 4 | * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24, | |
| 5 | * 1000 Lausanne, Switzerland, http://www.elca.ch | |
| 6 | * | |
| 7 | * EL4J is published under the GNU Lesser General Public License (LGPL) | |
| 8 | * Version 2.1. See http://www.gnu.org/licenses/ | |
| 9 | * | |
| 10 | * This program is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | * GNU Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * For alternative licensing, please contact info@elca.ch | |
| 16 | */ | |
| 17 | package ch.elca.el4j.maven.plugins.checkclipsehelper; | |
| 18 | ||
| 19 | import java.io.File; | |
| 20 | import java.io.FileOutputStream; | |
| 21 | import java.io.IOException; | |
| 22 | import java.util.Properties; | |
| 23 | ||
| 24 | import org.apache.maven.plugin.AbstractMojo; | |
| 25 | import org.apache.maven.plugin.MojoExecutionException; | |
| 26 | import org.apache.maven.plugin.MojoFailureException; | |
| 27 | import org.apache.maven.project.MavenProject; | |
| 28 | import org.springframework.util.DefaultPropertiesPersister; | |
| 29 | import org.springframework.util.PropertiesPersister; | |
| 30 | import org.springframework.util.StringUtils; | |
| 31 | ||
| 32 | /** | |
| 33 | * Mojo to config files in the .settings directory. | |
| 34 | * | |
| 35 | * @svnLink $Revision: 3872 $;$Date: 2009-08-04 13:41:09 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/maven/plugins/maven-checkclipse-helper-plugin/src/main/java/ch/elca/el4j/maven/plugins/checkclipsehelper/CheckclipseConfigSettingsMojo.java $ | |
| 36 | * | |
| 37 | * @author Martin Zeltner (MZE) | |
| 38 | * | |
| 39 | * @goal checkclipse-config-settings | |
| 40 | * @requiresProject true | |
| 41 | */ | |
| 42 | 0 | public class CheckclipseConfigSettingsMojo extends AbstractMojo { |
| 43 | // Checkstyle: MemberName off | |
| 44 | ||
| 45 | /** | |
| 46 | * Path to checkclipse config file. | |
| 47 | * | |
| 48 | * @parameter expression="${checkclipsehelper.settingsConfigFilePath}" | |
| 49 | * default-value=".settings/de.mvmsoft.checkclipse.prefs" | |
| 50 | * @required | |
| 51 | */ | |
| 52 | protected File settingsConfigFilePath; | |
| 53 | ||
| 54 | /** | |
| 55 | * Flag to enable checkclipse. | |
| 56 | * | |
| 57 | * @parameter expression="${checkclipsehelper.enableCheckclipse}" | |
| 58 | * default-value="true" | |
| 59 | * @required | |
| 60 | */ | |
| 61 | protected boolean enableCheckclipse; | |
| 62 | ||
| 63 | /** | |
| 64 | * Flag to use the project classloader to execute checkstyle checks. | |
| 65 | * | |
| 66 | * @parameter expression="${checkclipsehelper.useProjectClassloader}" | |
| 67 | * default-value="true" | |
| 68 | * @required | |
| 69 | */ | |
| 70 | protected boolean useProjectClassloader; | |
| 71 | ||
| 72 | /** | |
| 73 | * The maven project. | |
| 74 | * | |
| 75 | * @parameter expression="${project}" | |
| 76 | * @required | |
| 77 | * @readonly | |
| 78 | */ | |
| 79 | protected MavenProject project; | |
| 80 | ||
| 81 | // Checkstyle: MemberName on | |
| 82 | ||
| 83 | /** | |
| 84 | * Util to save property files. | |
| 85 | */ | |
| 86 | 0 | protected final PropertiesPersister m_persister |
| 87 | = new DefaultPropertiesPersister(); | |
| 88 | ||
| 89 | /** | |
| 90 | * {@inheritDoc} | |
| 91 | */ | |
| 92 | public void execute() throws MojoExecutionException, MojoFailureException { | |
| 93 | 0 | String projectPackaging = project.getPackaging(); |
| 94 | 0 | if (!StringUtils.hasText(projectPackaging) |
| 95 | || projectPackaging.contains("pom")) { | |
| 96 | 0 | getLog().info("No checkclipse settings config for pom project."); |
| 97 | 0 | return; |
| 98 | } | |
| 99 | ||
| 100 | 0 | if (settingsConfigFilePath.isDirectory()) { |
| 101 | 0 | throw new MojoExecutionException("Given file path " |
| 102 | + settingsConfigFilePath.getAbsolutePath() | |
| 103 | + " is a directory!"); | |
| 104 | } | |
| 105 | 0 | getLog().info( |
| 106 | "Writing file " + settingsConfigFilePath.getAbsolutePath()); | |
| 107 | ||
| 108 | // Create dir of config file if it does not exist | |
| 109 | 0 | String filePath = settingsConfigFilePath.getPath(); |
| 110 | 0 | String dirPath = filePath.substring(0, |
| 111 | filePath.lastIndexOf(java.io.File.separatorChar)); | |
| 112 | 0 | java.io.File dir = new java.io.File(dirPath); |
| 113 | 0 | if (!dir.isDirectory()) { |
| 114 | 0 | getLog().info("Directory " + dir.getAbsolutePath() + " does not " |
| 115 | + "exist. Will try to create it."); | |
| 116 | 0 | dir.mkdirs(); |
| 117 | } | |
| 118 | ||
| 119 | 0 | Properties props = new Properties(); |
| 120 | 0 | props.setProperty("enabled", Boolean.toString(enableCheckclipse)); |
| 121 | 0 | props.setProperty("projectclassloader", |
| 122 | Boolean.toString(useProjectClassloader)); | |
| 123 | try { | |
| 124 | 0 | m_persister.store(props, |
| 125 | new FileOutputStream(settingsConfigFilePath), | |
| 126 | "Checkclipse settings config file"); | |
| 127 | 0 | } catch (IOException e) { |
| 128 | 0 | throw new MojoExecutionException( |
| 129 | "Can not write file to given file path " | |
| 130 | + settingsConfigFilePath.getAbsolutePath(), e); | |
| 131 | 0 | } |
| 132 | 0 | } |
| 133 | } |