<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jp.hondoh</groupId> <artifactId>hondoh-swap-plugin</artifactId> <version>1.0</version> <packaging>maven-plugin</packaging> <name>hondoh-swap-plugin</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.maven.plugin-testing</groupId> <artifactId>maven-plugin-testing-harness</artifactId> <version>2.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.sonatype.aether</groupId> <artifactId>aether-api</artifactId> <version>1.13.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.sonatype.aether</groupId> <artifactId>aether-util</artifactId> <version>1.13.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-embedder</artifactId> <version>3.0.5</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.14</version> <configuration> <reuseForks>false</reuseForks> </configuration> </plugin> </plugins> </build> </project>
package jp.hondoh.maven.swap.plugin; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; /** * swap files * * @goal swapfiles * @phase process-test-classes * @author Atsushi HONDOH (kagyuu@hondou.homedns.org) */ public class SwapMojo extends AbstractMojo { /** * @parameter expression="${from}" * @required */ private String from; /** * @parameter expression="${to}" * @required */ private String to; /** * @parameter expression="${ifProp}" */ private String ifProp = null; /** * @parameter expression="${ifEnv}" */ private String ifEnv = null; /** * @parameter expression="${is}" */ private String is = null; /** * Execute. * * @throws MojoExecutionException predictable error * @throws MojoFailureException unpredictable error */ public void execute() throws MojoExecutionException, MojoFailureException { getLog().info(from + "\n <-> " + to); if (null != ifEnv) { String env = System.getenv(ifEnv); getLog().info("ENV " + ifEnv + " = " + env); if (!env.matches(is.trim())) { getLog().info(ifEnv + " not match " + is + ". Do nothing."); return; } } if (null != ifProp) { String env = System.getProperty(ifProp); getLog().info("PROP " + ifProp + " = " + env); if (!env.matches(is.trim())) { getLog().info(ifProp + " doesn't not match \"" + is + "\". Do nothing."); return; } } try { getLog().info("swap start"); File toFile = new File(to); File fromFile = new File(from); byte[] fromContents = readAll(fromFile); if (toFile.exists()) { byte[] toContents = readAll(toFile); writeAll(fromFile, toContents); } writeAll(toFile, fromContents); } catch (IOException ex) { getLog().error(ex.getMessage()); throw new MojoExecutionException("swap failed.", ex); } catch (Throwable th) { getLog().error(th.getMessage()); throw new MojoFailureException("swap failed", th); } } /** * read all contents. * Since it is better to run on Java 5 now, I didn't use NIO2. * @param f the file path. * @throws IOException read failed. */ private byte[] readAll(final File f) throws IOException { InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(f)); ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int size; while ((size = in.read(buf)) > 0) { bout.write(buf, 0, size); } return bout.toByteArray(); } finally { if (null != in) { try { in.close(); } catch (Exception ex) { ex = null; } } } } /** * write contens to path. * Since it is better to run on Java 5 now, I didn't use NIO2. * @param f the file path. * @param contents contnets. * @throws IOException write failed */ private void writeAll(final File f, final byte[] contents) throws IOException { OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(f)); out.write(contents); } finally { if (null != out) { try { out.close(); } catch (Exception ex) { ex = null; } } } } }
/** * @parameter expression="${from}" * @required */ private String from; /** * @parameter expression="${verbose}" default-value="false" */ private boolean verbose;
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jp.hondo</groupId> <artifactId>maven-swap-plugin-test</artifactId> <version>1.0</version> <build> <plugins> <plugin> <groupId>jp.hondoh</groupId> <artifactId>hondoh-swap-plugin</artifactId> <version>1.0</version> <executions> <execution> <goals> <goal>swapfiles</goal> </goals> <configuration> <ifProp>os.name</ifProp> <is>Mac.*</is> <from>${basedir}/target/test-classes/app.properties</from> <to>${basedir}/target/test-classes/app_macos.properties</to> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
SwapMojo mojo = (SwapMojo) lookupMojo("goal", new File(pom)); mojo.execute();
でテストが実行される
package jp.hondoh.maven.swap.plugin; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import static org.codehaus.plexus.PlexusTestCase.getBasedir; import org.junit.Test; import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.equalTo; /** * test swap mojo. * @author Atsushi HONDOH (kagyuu@hondou.homedns.org) */ public class SwapMojoTest extends AbstractMojoTestCase { @Test public void testExecuteSwapEjbPersistenceXml() throws Exception { String main1 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml")); String test1 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml")); File testPom = new File(getBasedir(), "src/test/resources/pom_test1.xml"); SwapMojo mojo = (SwapMojo) lookupMojo("swapfiles", testPom); mojo.execute(); String main2 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml")); String test2 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml")); // Swaped assertThat(main2, is(equalTo(test1))); assertThat(test2, is(equalTo(main1))); } @Test public void testExecuteEnvIsMatch() throws Exception { String main1 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml")); String test1 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml")); File testPom = new File(getBasedir(), "src/test/resources/pom_test2.xml"); SwapMojo mojo = (SwapMojo) lookupMojo("swapfiles", testPom); mojo.execute(); String main2 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml")); String test2 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml")); // Swaped ( if run on Mac ) assertThat(main2, is(equalTo(test1))); assertThat(test2, is(equalTo(main1))); } @Test public void testExecuteEnvIsNotMatch() throws Exception { String main1 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml")); String test1 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml")); File testPom = new File(getBasedir(), "src/test/resources/pom_test3.xml"); SwapMojo mojo = (SwapMojo) lookupMojo("swapfiles", testPom); mojo.execute(); String main2 = getContents(new File(getBasedir(), "target/classes/META-INF/persistence.xml")); String test2 = getContents(new File(getBasedir(), "target/test-classes/META-INF/persistence.xml")); // Not swaped ( if run on Linux ) assertThat(main2, is(equalTo(main1))); assertThat(test2, is(equalTo(test1))); } private String getContents(File f) throws IOException { BufferedReader br = new BufferedReader(new FileReader(f)); String line = br.readLine(); br.close(); return line; } }