Unable to set directory as resource/source in Maven Custom Plugin - maven

I have been unable to set a folder as source/resource using the most common code, copied and pasted from Apache repositories into a custom Maven Plugin:
Main/Mojo:
package com.example;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;
import java.io.File;
import static org.twdata.maven.mojoexecutor.MojoExecutor.*;
/**
* Goal that sets directory as source/resource.
*/
#Mojo(name = "set-some-dir-as-source", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public class SetDirectoryAsSourceMojo extends AbstractMojo {
#Component
private MavenSession mavenSession;
#Component
private BuildPluginManager pluginManager;
#Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
/**
* Output location.
*/
#Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}/some-folder/generated")
protected File outputDirectory;
/**
* Main entry into mojo.
*
* #throws MojoExecutionException
* #throws MojoFailureException
*/
#Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Executing Mark Directory as Source");
executeMojo(
plugin(
groupId("org.codehaus.mojo"),
artifactId("build-helper-maven-plugin"),
version("3.3.0")
),
goal("add-source"),
configuration(
element(name("sources"),
element(name("source"), getOutputDirectory().getPath())
)
),
executionEnvironment(
project,
mavenSession,
pluginManager
)
);
getLog().info("Executing Mark Directory as Resource");
executeMojo(
plugin(
groupId("org.codehaus.mojo"),
artifactId("build-helper-maven-plugin"),
version("3.3.0")
),
goal("add-resource"),
configuration(
element(name("resources"),
element(name("resource"),
element(name("directory"), getOutputDirectory().getPath())
)
)
),
executionEnvironment(
project,
mavenSession,
pluginManager
)
);
project.addCompileSourceRoot(getOutputDirectory().getPath());
project.addCompileSourceRoot(getOutputDirectory().getAbsolutePath());
if (getLog().isInfoEnabled()) {
getLog().info("Source directory: " + getOutputDirectory().getPath() + " added.");
getLog().info("Source directory: " + getOutputDirectory().getAbsolutePath() + " added.");
}
Resource resource = new Resource();
resource.setTargetPath(getOutputDirectory().getPath());
resource.setTargetPath(getOutputDirectory().getAbsolutePath());
getProject().addCompileSourceRoot(getOutputDirectory().getAbsolutePath());
if (getLog().isInfoEnabled()) {
getLog().info("Resource directory: " + getOutputDirectory().getPath() + " added.");
getLog().info("Resource directory: " + getOutputDirectory().getAbsolutePath() + " added.");
}
}
/**
* #return Returns the Maven project.
*/
public MavenProject getProject() {
return project;
}
/**
* #return Returns the outputDirectory.
*/
public File getOutputDirectory() {
return this.outputDirectory;
}
/**
* #param theOutputDirectory The outputDirectory to set.
*/
public void setOutputDirectory(File theOutputDirectory) {
this.outputDirectory = theOutputDirectory;
}
}
In the example above, I am using executeMojo from:
https://github.com/mojo-executor/mojo-executor
And also doing it programatically in the last few lines.
Project pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>custom-set-source-maven-plugin</artifactId>
<version>0.0.1</version>
<packaging>maven-plugin</packaging>
<name>custom-set-source-maven-plugin</name>
<url>http://maven.apache.org</url>
<organization>
<name>The Example</name>
<url>https://www.example.com/</url>
</organization>
<properties>
<java.version>1.8</java.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven-plugin-api.version>3.8.4</maven-plugin-api.version>
<maven-plugin-annotations.version>3.6.4</maven-plugin-annotations.version>
<maven-project.version>2.2.1</maven-project.version>
<maven-plugin-plugin.version>3.6.4</maven-plugin-plugin.version>
<maven-site-plugin.version>3.11.0</maven-site-plugin.version>
<maven-dependency-plugin.version>3.2.0</maven-dependency-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven-plugin-api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${maven-plugin-annotations.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>${maven-project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.twdata.maven</groupId>
<artifactId>mojo-executor</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>${maven-plugin-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>${maven-site-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
If I simply declare the plugin in my project, from org.codehaus.mojo:build-helper-maven-plugin, it works:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>add-element-templates-as-source</id>
<phase>generate-resources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/main/resources/processes/some_folder/generated</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Am I missing any descriptor in my custom plugin? A way to tell the IDE that the directory has been set that I am missing? I can't seem to find anything in the Mojohaus source codes at:
https://github.com/mojohaus/build-helper-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java
or
https://github.com/mojohaus/build-helper-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java

While building I was seeing following error:
[ERROR]
Some dependencies of Maven Plugins are expected to be in provided scope.
Please make sure that dependencies listed below declared in POM
have set '<scope>provided</scope>' as well.
The following dependencies are in wrong scope:
* org.apache.maven:maven-plugin-api:jar:3.8.4:compile
* org.apache.maven:maven-model:jar:3.8.4:compile
* org.apache.maven:maven-artifact:jar:3.8.4:compile
* org.apache.maven:maven-project:jar:2.2.1:compile
* org.apache.maven:maven-settings:jar:2.2.1:compile
* org.apache.maven:maven-profile:jar:2.2.1:compile
* org.apache.maven:maven-artifact-manager:jar:2.2.1:compile
* org.apache.maven:maven-plugin-registry:jar:2.2.1:compile
* org.apache.maven:maven-core:jar:3.1.1:compile
* org.apache.maven:maven-settings-builder:jar:3.1.1:compile
* org.apache.maven:maven-model-builder:jar:3.1.1:compile
* org.apache.maven:maven-repository-metadata:jar:3.1.1:compile
* org.apache.maven:maven-aether-provider:jar:3.1.1:compile
Some changes needed in the POM file to get it working.
There was warning with Component annotation above Maven Session. Changed it as below:
#Parameter( defaultValue = "${session}", readonly = true )
private MavenSession mavenSession;
After above changes able to compile and execute the code using the command below:
mvn install com.example:custom-set-source-maven-plugin:0.0.1:set-some-dir-as-source install
For some reason the code was complaining about the outputDirectory.
I have the fixed code at https://github.com/gopinnath/custom-set-source-maven-plugin
Can be executed with below maven command:
install com.example:custom-set-source-maven-plugin:0.0.1:set-some-dir-as-source install exec:java -Dexec.mainClass="Example"

Related

How can I deploy my Spring Boot Application in Equinox OSGi Container. Getting BundleException: Error loading bundle activator

My Activator class:
package com.package.actprovider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceRegistration;
import com.package.Application;
import org.osgi.framework.ServiceEvent;
/**
* This class implements a simple bundle that utilizes the OSGi
* framework's event mechanism to listen for service events. Upontting
* receiving a service event, it prints out the event's details.
**/
public class Activator implements BundleActivator , ServiceListener{
private ServiceRegistration registration;
//private Application application;
/**
* Implements BundleActivator.start(). Prints
* a message and adds itself to the bundle context as a service
* listener.
* #param context the framework context for the bundle.
**/
#Override
public void start(BundleContext context)
{
System.out.println("Starting to listen for service events.++++");
context.addServiceListener(this);
}
/**
* Implements BundleActivator.stop(). Prints
* a message and removes itself from the bundle context as a
* service listener.
* #param context the framework context for the bundle.
**/
#Override
public void stop(BundleContext context)
{
context.removeServiceListener(this);
System.out.println("Stopped listening for service events.");
}
/**
* Implements ServiceListener.serviceChanged().
* Prints the details of any service event from the framework.
* #param event the fired service event.
**/
public void serviceChanged(ServiceEvent event)
{
String[] objectClass = (String[])
event.getServiceReference().getProperty("objectClass");
if (event.getType() == ServiceEvent.REGISTERED)
{
System.out.println(
"Ex1: Service of type " + objectClass[0] + " registered.");
}
else if (event.getType() == ServiceEvent.UNREGISTERING)
{
System.out.println(
"Ex1: Service of type " + objectClass[0] + " unregistered.");
}
else if (event.getType() == ServiceEvent.MODIFIED)
{
System.out.println(
"Ex1: Service of type " + objectClass[0] + " modified.");
}
}
}
MANIFEST.MF file that is read by the equinox container:
Manifest-Version: 1.0
Bundle-Description: A bundle that displays messages at startup and whe
n service events occur
Bundle-Name: Service listener example
Bundle-Version: 1.0.0
Bundle-Activator: com.package.actprovider.Activator
Bundle-Vendor: Apache Felix
Import-Package: org.osgi.framework
Created-By: 1.8.0_101 (Oracle Corporation)
Here Bundle-Activator: com.package.actprovider.Activator , but the activator.class file creating at /BOOT-INF/classes/package com.package.actprovider.Activator
my pom.xml is as follows
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.package</groupId>
<artifactId>myfoo</artifactId>
<version>0.1.0</version>
<packaging>bundle</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath />
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
</plugin>
<!-- tag::plugin[] -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.9</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId>
<configuration> <archive> <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive> </configuration> </plugin> -->
<plugin>
\
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<dependency>
<groupId>sdp-api-java</groupId>
<artifactId>sdp-api-java</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>
please suggest how can I add spring-boot bundle inside osgi container
Spring boot and OSGi are mutually exclusive. You can use spring to a degree but even that works badly.
So the best practice is to build OSGi applications based on declarative services or blueprint. You might also take a look at Apache Karaf for support of technologies like JPA, servlets ...
It will be a little more effort than in spring boot but you should be able to solve the same problems.

Maven plugin - modifying configuration of another plugin

I'm developing custom maven plugin.
My plugin requires a specific configuration for the surefire plugin. As result, as part of my MOJO I'm searching for 'surefire' and if it is present I'm trying to modify its configuration.
My problem is that the configuration is not used.
Here's most of my code:
package io.kuku.agents.plugin;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
/**
* Initialize the integration with the Testing Framework.
*
* #phase test
* #goal initialize-test-listener
* #since 1.0.0
*/
public class SetupMojo extends AbstractKukusMojo {
boolean hasJunit = false;
boolean hasTestNG = false;
public void execute() throws MojoExecutionException, MojoFailureException {
analyzeDependencies();
Plugin surefirePlugin = lookupPlugin("org.apache.maven.plugins:maven-surefire-plugin");
Object config = updateConfiguration(hasJunit, hasTestNG, surefirePlugin.getConfiguration());
surefirePlugin.setConfiguration(config);
List<PluginExecution> executions = surefirePlugin.getExecutions();
for (PluginExecution execution : executions) {
if (execution.getId().equals("default-test")) {
System.out.println("Setting DEFAULT-TEST");
config = updateConfiguration(hasJunit, hasTestNG, execution.getConfiguration());
execution.setConfiguration(config);
break;
}
}
}
private void analyzeDependencies() {
List dependencies = this.project.getDependencies();
for (int i = 0; i < dependencies.size(); i++) {
Dependency dependency = (Dependency) dependencies.get(i);
if (dependency.getArtifactId().equalsIgnoreCase("junit")) {
hasJunit = true;
if (hasJunit && hasTestNG)
break;
else
continue;
}
if (dependency.getArtifactId().equalsIgnoreCase("testng")) {
hasTestNG = true;
if (hasJunit && hasTestNG)
break;
else
continue;
}
}
}
private Object updateConfiguration(boolean hasJunit, boolean hasTestNG, Object configuration) throws MojoExecutionException {
if (configuration == null)
configuration = new Xpp3Dom("configuration");
if (configuration instanceof Xpp3Dom) {
Xpp3Dom xml = (Xpp3Dom) configuration;
Xpp3Dom properties = xml.getChild("properties");
if (properties == null)
{
properties = new Xpp3Dom("properties");
xml.addChild(properties);
}
Xpp3Dom[] property = properties.getChildren("property");
//My logic goes here
...
...
}
return configuration;
}
}
I'll appreciate your help.
N.
EDIT - This is my parent pom:
<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>sllistenertest</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Sl Listener Test (Parent)</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>io.kuku.on-premise.agents.plugin</groupId>
<artifactId>kuku-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<customerid>nadav2</customerid>
<server>https://fake.kuku.co/api</server>
<appName>fake-app-name</appName>
<moduleName>fake-module-name</moduleName>
<workspacepath>${project.basedir}</workspacepath>
<build>52</build>
<branch>fake-branch</branch>
<packagesincluded>*fklistenertest*</packagesincluded>
<packagesexcluded>com.fake.excluded.*</packagesexcluded>
<filesincluded>*.class</filesincluded>
<logLevel>INFO</logLevel>
<logFolder>c:\fake-log-folder</logFolder>
<logToFile>true</logToFile>
<logEnabled>true</logEnabled>
</configuration>
<executions>
<execution>
<id>a1</id>
<goals>
<goal>build-scanner</goal>
</goals>
</execution>
<execution>
<id>a2</id>
<goals>
<goal>test-listener</goal>
</goals>
</execution>
<execution>
<id>a3</id>
<goals>
<goal>initialize-test-listener</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<!--<configuration>
<properties>
<property>
<name>listener</name>
<value>io.kuku.onpremise.agents.java.agent.integrations.testng.TestListener</value>
</property>
</properties>
<additionalClasspathElements>
<additionalClasspathElement>C:\Temp\kuku-java-1.3.160\artifacts\kuku-api-1.3.160.jar</additionalClasspathElement>
</additionalClasspathElements>
</configuration>-->
<executions>
<execution>
<id>default-test</id>
<phase>none</phase>
</execution>
<execution>
<id>run-after-antrun</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<executions>
<execution>
<id>default-test</id>
<phase>none</phase>
</execution>
<execution>
<id>run-after-antrun</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<modules>
<module>Only Junit</module>
<module>Only TestNG</module>
<module>Both</module>
</modules>
</project>
This is the pom for one of the children:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>sllistenertest</groupId>
<artifactId>parent-artifact</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>sllistenertest</groupId>
<artifactId>onlyjunit</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Only JUnit</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.kuku.on-premise.agents.plugin</groupId>
<artifactId>kuku-maven-plugin</artifactId>
<version>1.0.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
</plugin>
</plugins>
</build>
</project>
You should take a look here:
for JUnit
https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html#Using_Custom_Listeners_and_Reporters
For TestNG:
https://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html#Using_Custom_Listeners_and_Reporters
So i don't see the requirement to implement a plugin...

ContextConfiguration does not seem to load my beans for TestNg testcase

I am new to TestNg. I am trying to test one sample hello world program.
Following is my project structure
Source Package
com.mycompany.mavenproject1 =>HelloWorld.java
Test Package
com.test => NewTestConfig.java & NewTestNGTest.java
Helloworld.java
package com.mycompany.mavenproject1;
public class HelloWorld {
String msg = "test";
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
NewTestConfig.java
package com.test;
import com.mycompany.mavenproject1.HelloWorld;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#Configuration
public class NewTestConfig {
#Bean
public HelloWorld helloWorldBean() {
HelloWorld hw = new HelloWorld();
return hw;
}
}
NewTestNGTest.java
package com.test;
import com.mycompany.mavenproject1.HelloWorld;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.springframework.test.context.ContextConfiguration;
#Test
#ContextConfiguration(classes={ NewTestConfig.class })
public class NewTestNGTest {
#Autowired
HelloWorld hw ;
#Test
public void exampleOfTestNgMaven() {
String s = hw.getMsg();
System.out.println("This is TestNG-Maven Example" + s);
}
#BeforeClass
public static void setUpClass() throws Exception {
}
#AfterClass
public static void tearDownClass() throws Exception {
}
#BeforeMethod
public void setUpMethod() throws Exception {
}
#AfterMethod
public void tearDownMethod() throws Exception {
}
}
My pom.xml looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>mavenproject1</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
When I try to run the test I get NullpointerException (at hw.getMsg();)
Error stack:
Test set: com.test.NewTestNGTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.209 sec <<< FAILURE!
exampleOfTestNgMaven(com.test.NewTestNGTest) Time elapsed: 0.003 sec <<< FAILURE!
java.lang.NullPointerException
at com.test.NewTestNGTest.exampleOfTestNgMaven(NewTestNGTest.java:25)
I tried to run test in debug mode, I always get HelloWorld bean as null. So eventually test fails due to NullPointerException. I tried many other variations such as instead of config class tried using applicationContext.xml but my bean never gets injected.
For last 2 days I am struggling to make simple test pass but unable to figure out what's going wrong. Why my context is not getting loaded for test?
Any help is greatly appreciated.
Thanks
Manisha

Deploying running word count on Spark

I have a problem deploying the running word count example for Spark Streaming. I am trying to deploy the same file that is provided with Spark examples, but I want to build and deploy this specific example as a stand alone application.
My file looks like this:
package test;
import scala.Tuple2;
import com.google.common.collect.Lists;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.StorageLevels;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import java.util.regex.Pattern;
public final class JavaNetworkWordCount {
private static final Pattern SPACE = Pattern.compile(" ");
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("Usage: JavaNetworkWordCount <hostname> <port>");
System.exit(1);
}
// Create the context with a 1 second batch size
SparkConf sparkConf = new SparkConf()
.setAppName("JavaNetworkWordCount");
JavaStreamingContext ssc = new JavaStreamingContext(sparkConf,
Durations.seconds(1));
// Create a JavaReceiverInputDStream on target ip:port and count the
// words in input stream of \n delimited text (eg. generated by 'nc')
// Note that no duplication in storage level only for running locally.
// Replication necessary in distributed scenario for fault tolerance.
JavaReceiverInputDStream<String> lines = ssc.socketTextStream(args[0],
Integer.parseInt(args[1]), StorageLevels.MEMORY_AND_DISK_SER);
JavaDStream<String> words = lines
.flatMap(new FlatMapFunction<String, String>() {
public Iterable<String> call(String x) {
return Lists.newArrayList(SPACE.split(x));
}
});
JavaPairDStream<String, Integer> wordCounts = words.mapToPair(
new PairFunction<String, String, Integer>() {
public Tuple2<String, Integer> call(String s) {
return new Tuple2<String, Integer>(s, 1);
}
}).reduceByKey(new Function2<Integer, Integer, Integer>() {
public Integer call(Integer i1, Integer i2) {
return i1 + i2;
}
});
wordCounts.print();
ssc.start();
ssc.awaitTermination();
}
}
And my POM looks like this:
<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>io.tester</groupId>
<artifactId>streamer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>streamer</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.binary.version>2.11</scala.binary.version>
<spark.version>1.4.1</spark.version>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>test.JavaNetworkWordCount</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
The error that I get is:
java.lang.NoClassDefFoundError: com/google/common/collect/Lists
I look through my jar that I built with maven. It has a with-dependencies appended to it, but it doesn't seem to actually have any dependencies in it. I run it via mvn assembly:single. What am I doing wrong?
As the maven-assembly-plugin indicates
If your project wants to package your artifact in an uber-jar, the assembly plugin provides only basic support. For more control, use the Maven Shade Plugin
you can try to use the maven-shade-plugin. Try to replace the maven-assembly-plugin plugin tag with:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>test.JavaNetworkWordCount</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
This should hopefully create a fat jar containing all your dependencies.
I got it figured out. I had two problems. One, I didn't notice that I had the provided clause (stupid cut and paste error). The second problem I had was that one of the dependencies pulled in was signed and I needed to explicitly exclude the signature files. My end product that actually works looks like this in case someone else is having this problem:
<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>io.tester</groupId>
<artifactId>streamer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>streamer</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.binary.version>2.11</scala.binary.version>
<spark.version>1.4.1</spark.version>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.tester.streamer.JavaNetworkWordCount</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

maven plugin descriptor not getting generated

I'm creating a maven plugin, MVN clean install build succeeds but plugin.xml is not getting generated.
#Mojo( name = "cover", defaultPhase = LifecyclePhase.POST_INTEGRATION_TEST)
public class RunCoverage extends AbstractMojo
{
#Parameter( property = "cover.wadl", defaultValue = "test")
private String wadl;
#Parameter( property = "cover.endpoints",defaultValue = "test")
private String endpoints;
#Override
public void execute() throws MojoExecutionException
{
<somecode>
}
}
And the pom.xml is
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>end-point-test-coverage</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<executions>
<execution>
<id>default-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
<phase>process-classes</phase>
</execution>
<execution>
<id>help-descriptor</id>
<goals>
<goal>helpmojo</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Maven clean install doesn't generate plugin.xml
When used in a dependent project, I'm getting the following error
Failed to parse plugin descriptor for it.gruppopam.common:end-point-test-coverage:1 (/home/d/.m2/repository/it/common/end-point-test-coverage/1/end-point-test-coverage-1.jar): No plugin descriptor found at META-INF/maven/plugin.xml -> [Help 1]
[ERROR]
First i would try to set the packaging type to maven-plugin instead of the default which is jar. Furthermore i would suggest to use more up-to-date versions of plugins (maven-compiler-plugin: 3.1) and use a more up-to-date version of maven-plugin-api (3.0? but not 2.0).
you have to remember to change it in your pom.xml to:
<packaging>maven-plugin</packaging>
it is by default:
<packaging>jar</packaging>

Resources