org.apache.maven.plugin.Mojo Java Examples

The following examples show how to use org.apache.maven.plugin.Mojo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FailOnErrorTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testExecuteCommandScriptContinueOnError() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-script-continueOnError-pom.xml");

    executeCommandsMojo.execute();

    // Read the attribute
    final ModelNode address = ServerOperations.createAddress("system-property", "scriptContinueOnError");
    final ModelNode op = ServerOperations.createReadAttributeOperation(address, "value");
    final ModelNode result = executeOperation(op);

    try {
        assertEquals("continue on error", ServerOperations.readResultAsString(result));
    } finally {
        // Clean up the property
        executeOperation(ServerOperations.createRemoveOperation(address));
    }
}
 
Example #2
Source File: FailOnErrorTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testExecuteCommandScriptFailOnError() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-script-failOnError-pom.xml");

    try {
        executeCommandsMojo.execute();
        fail("MojoExecutionException expected.");
    } catch (MojoExecutionException e) {
        assertEquals(CommandLineException.class, e.getCause().getClass());
    }
    final ModelNode address = ServerOperations.createAddress("system-property", "scriptFailOnError");
    final ModelNode op = ServerOperations.createReadAttributeOperation(address, "value");
    final ModelNode result = executeOperation(op);
    try {
        assertEquals("initial value", ServerOperations.readResultAsString(result));
    } finally {
        // Remove the system property
        executeOperation(ServerOperations.createRemoveOperation(address));
    }
}
 
Example #3
Source File: FailOnErrorTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testExecuteCommandsContinueOnError() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-commands-continueOnError-pom.xml");

    executeCommandsMojo.execute();

    // Read the attribute
    final ModelNode address = ServerOperations.createAddress("system-property", "propertyContinueOnError");
    final ModelNode op = ServerOperations.createReadAttributeOperation(address, "value");
    final ModelNode result = executeOperation(op);

    try {
        assertEquals("continue on error", ServerOperations.readResultAsString(result));
    } finally {
        // Clean up the property
        executeOperation(ServerOperations.createRemoveOperation(address));
    }
}
 
Example #4
Source File: FailOnErrorTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testExecuteCommandsForkCmdFailOnError() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-commands-fork-cmd-failOnError-pom.xml");
    // Set the JBoss home field so commands will be executed in a new process
    setValue(executeCommandsMojo, "jbossHome", TestEnvironment.WILDFLY_HOME.toString());


    try {
        executeCommandsMojo.execute();
        fail("MojoExecutionException expected.");
    } catch (MojoExecutionException ignore) {
    }
    // Read the attribute
    final ModelNode address = ServerOperations.createAddress("system-property", "propertyFailOnError.in.try");
    final ModelNode result = executeOperation(ServerOperations.createReadAttributeOperation(address, "value"));

    try {
        assertEquals("inside catch", ServerOperations.readResultAsString(result));
    } finally {
        // Remove the system property
        executeOperation(ServerOperations.createRemoveOperation(address));
    }
}
 
Example #5
Source File: FailOnErrorTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testExecuteCommandsForkOpFailOnError() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-commands-fork-op-failOnError-pom.xml");
    // Set the JBoss home field so commands will be executed in a new process
    setValue(executeCommandsMojo, "jbossHome", TestEnvironment.WILDFLY_HOME.toString());

    try {
        executeCommandsMojo.execute();
        fail("MojoExecutionException expected.");
    } catch (MojoExecutionException ignore) {
    }

    final ModelNode address = ServerOperations.createAddress("system-property", "propertyFailOnError");
    final ModelNode op = ServerOperations.createReadAttributeOperation(address, "value");
    final ModelNode result = executeOperation(op);
    try {
        assertEquals("initial value", ServerOperations.readResultAsString(result));
    } finally {
        // Remove the system property
        executeOperation(ServerOperations.createRemoveOperation(address));
    }
}
 
Example #6
Source File: FailOnErrorTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testExecuteCommandsFailOnError() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-commands-failOnError-pom.xml");

    try {
        executeCommandsMojo.execute();
        fail("MojoExecutionException expected.");
    } catch (MojoExecutionException e) {
        assertEquals(CommandLineException.class, e.getCause().getClass());
    }
    final ModelNode address = ServerOperations.createAddress("system-property", "propertyFailOnError");
    final ModelNode op = ServerOperations.createReadAttributeOperation(address, "value");
    final ModelNode result = executeOperation(op);
    try {
        assertEquals("initial value", ServerOperations.readResultAsString(result));
    } finally {
        // Remove the system property
        executeOperation(ServerOperations.createRemoveOperation(address));
    }
}
 
Example #7
Source File: ExecuteCommandsTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testExecuteBatchCommands() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-batch-commands-pom.xml");

    executeCommandsMojo.execute();

    // Read the attribute
    final ModelNode address = ServerOperations.createAddress("system-property", "org.wildfly.maven.plugin-batch");
    final ModelNode op = ServerOperations.createReadAttributeOperation(address, "value");
    final ModelNode result = executeOperation(op);
    assertEquals("true", ServerOperations.readResultAsString(result));

    // Clean up the property
    executeOperation(ServerOperations.createRemoveOperation(address));
}
 
Example #8
Source File: ExecuteCommandsTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testExecuteCommands() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-commands-pom.xml");

    executeCommandsMojo.execute();

    // Read the attribute
    ModelNode address = ServerOperations.createAddress("system-property", "org.wildfly.maven.plugin-exec-cmd");
    ModelNode op = ServerOperations.createReadAttributeOperation(address, "value");
    ModelNode result = executeOperation(op);
    assertEquals("true", ServerOperations.readResultAsString(result));

    // Clean up the property
    executeOperation(ServerOperations.createRemoveOperation(address));


    // Read the attribute
    address = ServerOperations.createAddress("system-property", "property2");
    op = ServerOperations.createReadAttributeOperation(address, "value");
    result = executeOperation(op);
    assertEquals("property 2", ServerOperations.readResultAsString(result));

    // Clean up the property
    executeOperation(ServerOperations.createRemoveOperation(address));
}
 
Example #9
Source File: ExecuteCommandsTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testExecuteForkCommands() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-commands-fork-pom.xml");
    setValue(executeCommandsMojo, "jbossHome", TestEnvironment.WILDFLY_HOME.toString());

    executeCommandsMojo.execute();

    // Read the attribute
    ModelNode address = ServerOperations.createAddress("system-property", "org.wildfly.maven.plugin-fork-cmd");
    ModelNode op = ServerOperations.createReadAttributeOperation(address, "value");
    ModelNode result = executeOperation(op);
    assertEquals("true", ServerOperations.readResultAsString(result));

    // Ensure the module has been added
    final Path moduleDir = Paths.get(TestEnvironment.WILDFLY_HOME.toString(), "modules", "org", "wildfly", "plugin", "tests", "main");
    assertTrue(String.format("Expected %s to exist.", moduleDir), Files.exists(moduleDir));
    assertTrue("Expected the module.xml to exist in " + moduleDir, Files.exists(moduleDir.resolve("module.xml")));
    assertTrue("Expected the test.jar to exist in " + moduleDir, Files.exists(moduleDir.resolve("test.jar")));

    // Clean up the property
    executeOperation(ServerOperations.createRemoveOperation(address));


    // Read the attribute
    address = ServerOperations.createAddress("system-property", "fork-command");
    op = ServerOperations.createReadAttributeOperation(address, "value");
    result = executeOperation(op);
    assertEquals("set", ServerOperations.readResultAsString(result));

    // Clean up the property
    executeOperation(ServerOperations.createRemoveOperation(address));

    // Remove the module
    deleteRecursively(TestEnvironment.WILDFLY_HOME.resolve("modules").resolve("org"));
}
 
Example #10
Source File: AbstractDropwizardMojoTest.java    From dropwizard-debpkg-maven-plugin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Mojo> T loadPlugin(final String goal, final URL config) throws Exception {
    final File pomFile = temporaryFolder.newFile();
    try (final InputStream in = config.openStream()) {
        FileUtils.copyInputStreamToFile(in, pomFile);
    }
    return (T) mojo.lookupMojo(goal, pomFile);
}
 
Example #11
Source File: NoClassesVisitorPluginTest.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
@Override
public void testExecute() throws Exception {
    final Mojo mojo = initMojo();
    mojo.execute();

    generatedCodeFixture.assertInterfaces();
}
 
Example #12
Source File: ByteBuddyMojoTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
private void execute(String goal, String target) throws Exception {
    InputStream in = ByteBuddyMojoTest.class.getResourceAsStream("/net/bytebuddy/test/" + target + ".pom.xml");
    if (in == null) {
        throw new AssertionError("Cannot find resource for: " + target);
    }
    try {
        File pom = File.createTempFile("maven", ".pom");
        OutputStream out = new FileOutputStream(pom);
        try {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
        } finally {
            out.close();
        }
        Mojo mojo = mojoRule.lookupMojo(goal, pom);
        if (goal.equals("transform")) {
            mojoRule.setVariableValueToObject(mojo, "outputDirectory", project.getAbsolutePath());
            mojoRule.setVariableValueToObject(mojo, "compileClasspathElements", Collections.emptyList());
        } else if (goal.equals("transform-test")) {
            mojoRule.setVariableValueToObject(mojo, "testOutputDirectory", project.getAbsolutePath());
            mojoRule.setVariableValueToObject(mojo, "testClasspathElements", Collections.emptyList());
        } else {
            throw new AssertionError("Unknown goal: " + goal);
        }
        mojoRule.setVariableValueToObject(mojo, "repositorySystem", repositorySystem);
        mojoRule.setVariableValueToObject(mojo, "groupId", FOO);
        mojoRule.setVariableValueToObject(mojo, "artifactId", BAR);
        mojoRule.setVariableValueToObject(mojo, "version", QUX);
        mojoRule.setVariableValueToObject(mojo, "packaging", JAR);
        mojo.setLog(new SilentLog());
        mojo.execute();
    } finally {
        in.close();
    }
}
 
Example #13
Source File: Pipeline.java    From wisdom with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new pipeline. Notice that the set of watchers cannot change.
 *  @param mojo    the 'run' mojo
 * @param baseDir the base directory of the watched project
 * @param list    the set of watchers plugged on the pipeline, the order of the list will be the notification order.
 * @param pomFileMonitoring flag enabling or disabling the pom file monitoring
 */
public Pipeline(Mojo mojo, File baseDir, List<? extends Watcher> list, boolean pomFileMonitoring) {
    this.mojo = mojo;
    this.baseDir = baseDir;
    this.pomFileMonitoring = pomFileMonitoring;
    mojo.getLog().debug("Initializing watch mode with " + list);
    watchers = new ArrayList<>();
    for (Object o : list) {
        watchers.add(new WatcherDelegate(o));
    }
}
 
Example #14
Source File: PipelinesTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoWatchers() throws Exception {
    MavenSession session = mock(MavenSession.class);
    when(session.getExecutionProperties()).thenReturn(new Properties());
    File baseDir = new File("target/junk");
    Mojo mojo = mock(Mojo.class);
    Log log = mock(Log.class);
    when(mojo.getLog()).thenReturn(log);
    Pipelines.watchers(session, baseDir, mojo, true);
}
 
Example #15
Source File: PipelineTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws IOException {
    FileUtils.forceMkdir(SOURCES);
    textWatcher = new SpyWatcher(SOURCES, "txt");
    mdWatcher = new SpyWatcher(SOURCES, "md");
    mojo = mock(Mojo.class);
    Log log = mock(Log.class);
    when(mojo.getLog()).thenReturn(log);
    pipeline = new Pipeline(mojo, FAKE, Arrays.asList(textWatcher, mdWatcher), false);
    pipeline.watch();
}
 
Example #16
Source File: DefensiveThreadFactoryTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    log = new CollectorLog();
    Mojo mojo = mock(Mojo.class);
    when(mojo.getLog()).thenReturn(log);
    factory = new DefensiveThreadFactory("test", mojo);
}
 
Example #17
Source File: ExecuteCommandsTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testExecuteCommandsFromOfflineScript() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-offline-script-pom.xml");
    setValue(executeCommandsMojo, "jbossHome", System.getProperty("jboss.home"));
    executeCommandsMojo.execute();
}
 
Example #18
Source File: ExecuteCommandsTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testExecuteOfflineCommands() throws Exception {

    final Mojo executeCommandsMojo = lookupMojoAndVerify("execute-commands", "execute-commands-offline-pom.xml");
    setValue(executeCommandsMojo, "jbossHome", System.getProperty("jboss.home"));
    executeCommandsMojo.execute();
}
 
Example #19
Source File: MavenPluginHelper.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
public <T> T getMojo (String pom, File dir, String execName, Class<T> clazz) throws Exception {
    MavenProject project = newMavenProject(pom, dir);
    MavenSession session = newMavenSession(project);
    MojoExecution execution = newMojoExecution(execName);
    Mojo mojo = lookupConfiguredMojo(session, execution);
    assertNotNull(mojo);
    return clazz.cast(mojo);
}
 
Example #20
Source File: AbstractWildFlyMojoTest.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Configures a MOJO based on the goal and the pom file then verifies the goal MOJO was found.
 *
 * @param goal     the name of the goal being tested
 * @param fileName the name of the POM file to be used during testing
 *
 * @return the MOJO object under test
 *
 * @throws java.lang.AssertionError if the MOJO was not found
 */
public <T extends Mojo> T lookupMojoAndVerify(final String goal, final String fileName) throws Exception {
    final Path baseDir = Paths.get(BASE_CONFIG_DIR);
    assertTrue("Not a directory: " + BASE_CONFIG_DIR, Files.exists(baseDir));
    final Path pom = Paths.get(BASE_CONFIG_DIR, fileName);
    assertTrue(Files.exists(pom));
    MavenProject project = readMavenProject(pom);
    @SuppressWarnings("unchecked")
    T mojo = (T) rule.lookupConfiguredMojo(project, goal);
    assertNotNull(mojo);
    setDefaultEnvironment(mojo);
    return mojo;
}
 
Example #21
Source File: BetterAbstractMojoTestCase.java    From markdown-page-generator-plugin with MIT License 5 votes vote down vote up
/**
 * As {@link #lookupConfiguredMojo(MavenProject, String)} but taking the pom file
 * and creating the {@link MavenProject}.
 */
protected Mojo lookupConfiguredMojo(File pom, String goal) throws Exception {
    assertNotNull(pom);
    assertTrue(pom.exists());

    ProjectBuildingRequest buildingRequest = newMavenSession().getProjectBuildingRequest();
    ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
    MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();

    return lookupConfiguredMojo(project, goal);
}
 
Example #22
Source File: AstorMojoTest.java    From repairnator with MIT License 5 votes vote down vote up
public void testCardumenRepair() throws Exception {
	File f = getTestFile(projectPath + "pom.xml");
	Mojo mojo = lookupConfiguredMojo(f, "cardumen");
	assertNotNull( mojo );
	assertTrue("Wrong class: "+mojo, mojo instanceof CardumenMojo);

	CardumenMojo repair = (CardumenMojo) mojo;
	repair.execute();
}
 
Example #23
Source File: MavenPluginHelper.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
public <T> T getMojo (String pom, File dir, String execName, Class<T> clazz) throws Exception {
    MavenProject project = newMavenProject(pom, dir);
    MavenSession session = newMavenSession(project);
    MojoExecution execution = newMojoExecution(execName);
    Mojo mojo = lookupConfiguredMojo(session, execution);
    assertNotNull(mojo);
    return clazz.cast(mojo);
}
 
Example #24
Source File: BetterMojoRule.java    From swaggerhub-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * As {@link #lookupConfiguredMojo(MavenProject, String)} but taking the pom file
 * and creating the {@link MavenProject}.
 */
public Mojo lookupConfiguredMojo(File pom, String goal) throws Exception {
    assertNotNull(pom);
    assertTrue(pom.exists());

    ProjectBuildingRequest buildingRequest = newMavenSession().getProjectBuildingRequest();
    ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
    MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();

    return lookupConfiguredMojo(project, goal);
}
 
Example #25
Source File: GenerateMojoIT.java    From swagger-maven-plugin with MIT License 5 votes vote down vote up
/**
 * Tests the generated Swagger specifications based on the given method parameters.
 * 
 * @param folder The base folder of the expected output.
 * @param basename The basename for the generated Swagger specifications.
 * @param pom The name of the POM file to be used for the test.
 * @param prettyPrinted If true, the generated JSON Swagger specification should be pretty-printed.
 * @param outputFormats The output formats that should be generated and checked.
 * 
 * @throws Exception If a JSON parsing or file-read exception happens.
 */
private void testGenerate(String folder, String basename, String pom, boolean prettyPrinted, OutputFormat... outputFormats)
    throws Exception {

    Path output = Paths.get(folder);
    if (Files.exists(output)) {
        Files.walkFileTree(output, new DeleteVisitor());
    }

    Mojo mojo = rule.lookupMojo("generate", new File("src/test/resources/" + pom));
    mojo.execute();

    List<OutputFormat> formats = Arrays.asList(outputFormats);
    if (formats.contains(OutputFormat.JSON)) {
        Path expectedJson = Paths.get("src/test/resources/expectedOutput", folder, basename + ".json");
        Path generatedJson = Paths.get("target", folder, basename + ".json");
        assertJsonEquals(expectedJson, generatedJson);

        // We test the indentation by simply checking that the generated JSON contains 2 spaces
        String json = new String(Files.readAllBytes(generatedJson), StandardCharsets.UTF_8);
        Assert.assertEquals(prettyPrinted, json.contains("  "));
    }

    if (formats.contains(OutputFormat.YAML)) {
        Path expectedYaml = Paths.get("src/test/resources/expectedOutput", folder, basename + ".yaml");
        Path generatedYaml = Paths.get("target", folder, basename + ".yaml");
        assertYamlEquals(expectedYaml, generatedYaml);
    }
}
 
Example #26
Source File: NPEFixSafeMojoTest.java    From repairnator with MIT License 5 votes vote down vote up
public void testNPEFixRepair() throws Exception {
    File f = getTestFile(projectPath + "pom.xml");
    Mojo mojo = lookupConfiguredMojo(f, "npefix-safe");
    assertNotNull( mojo );
    assertTrue("Wrong class: "+mojo, mojo instanceof NPEFixSafeMojo);

    NPEFixSafeMojo repair = (NPEFixSafeMojo) mojo;
    repair.execute();

    assertTrue(repair.getResult().size() > 0);
}
 
Example #27
Source File: NopolMojoTest.java    From repairnator with MIT License 5 votes vote down vote up
public void testNopolRepair() throws Exception {
	File f = getTestFile(projectPath + "pom.xml");
	Mojo mojo = lookupConfiguredMojo(f, "nopol");
	assertNotNull( mojo );
	assertTrue("Wrong class: "+mojo, mojo instanceof NopolMojo);

	NopolMojo repair = (NopolMojo) mojo;
	repair.execute();

	assertEquals(NopolStatus.PATCH, repair.getResult().getNopolStatus());
}
 
Example #28
Source File: NPEfixMojoTest.java    From repairnator with MIT License 5 votes vote down vote up
public void testNPEFixRepair() throws Exception {
	File f = getTestFile(projectPath + "pom.xml");
	Mojo mojo = lookupConfiguredMojo(f, "npefix");
	assertNotNull( mojo );
	assertTrue("Wrong class: "+mojo, mojo instanceof NPEFixMojo);

	NPEFixMojo repair = (NPEFixMojo) mojo;
	repair.execute();

	assertTrue(repair.getResult().size() > 0);
}
 
Example #29
Source File: AstorMojoTest.java    From repairnator with MIT License 5 votes vote down vote up
public void testGenProgRepair() throws Exception {
	File f = getTestFile(projectPath + "pom.xml");
	Mojo mojo = lookupConfiguredMojo(f, "jGenProg");
	assertNotNull( mojo );
	assertTrue("Wrong class: "+mojo, mojo instanceof GenProgMojo);

	GenProgMojo repair = (GenProgMojo) mojo;
	repair.execute();
}
 
Example #30
Source File: AstorMojoTest.java    From repairnator with MIT License 5 votes vote down vote up
public void testKaliRepair() throws Exception {
	File f = getTestFile(projectPath + "pom.xml");
	Mojo mojo = lookupConfiguredMojo(f, "jKali");
	assertNotNull( mojo );
	assertTrue("Wrong class: "+mojo, mojo instanceof KaliMojo);

	KaliMojo repair = (KaliMojo) mojo;
	repair.execute();
}