I have 2 maven modules and tests as following:
Module1
`src/test/java/Parent`
Module2
`src/test/java/Test // Test extends Parent`
Module1 has rumtime dependency on Module1
When I run mvn test, I get below error:
org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.lang.NoClassDefFoundError: Parent
Any idea what I may be doing correct?
Compilation in eclipse succeeds.
Usually in Maven unit tests are limited by module and will not be propagated which means you can't use them in other module. Unfortunately Eclipse does this separation not 100% correct as you already realized.
You can if you need create a so called test-jar which contains the files from src/test/ which gives you the possibility to reuse those classes from an other module. This mean you need to add the following to your Module1:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
...
</project>
In you other module you need to add the dependency to this separately created artifact by using this:
<project>
...
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<type>test-jar</type>
<version>version</version>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
Related
As per the direction in the link, tried to build the JAR file for scheduling dataflow job in Airflow. Added relevant dependency in the dependency section and given main class name in the build section in the POM file. When I execute the dataflow job using below maven command then getting below error.
Could some one guide me to solve this issue.
Maven Command
- mvn package
- java -jar target/sample-1.0.0.jar
Error Message:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/beam/sdk/options/PipelineOptions
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.beam.sdk.options.PipelineOptions
My Pipeline File
public interface DefaultOptions extends PipelineOptions,GcpOptions,ApplicationNameOptions,DataflowPipelineDebugOptions,DataflowPipelineWorkerPoolOptions,
BigQueryOptions,GcsOptions,StreamingOptions,CloudDebuggerOptions,DataflowProfilingOptions,PubsubOptions {
My Pipeline Initialization in class file
PipelineOptionsFactory.register(DefaultOptions.class);
DefaultOptions options = PipelineOptionsFactory.fromArgs(args).withValidation().as(DefaultOptions.class);
options.setRunner(DataflowRunner.class);
options.setProject(options.getDataFlowProjectName());
options.setTempLocation(options.getDataFlowProjectTempLocation());
options.setGcpTempLocation(options.getDataFlowProjectTempLocation());
Pipeline p = Pipeline.create(options);
POM File:
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-runners-google-cloud-dataflow-java</artifactId>
<version>2.11.0</version>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>SampleWindowJar</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
java.lang.NoClassDefFoundError is throwing since you didn't copy the dependencies before packaging it to jar.While running the jar jvm is trying to load class org.apache.beam.sdk.options.PipelineOptions from the dependencies. To fix that you need to add one more plugin maven-dependency-plugin
Sample plugin section in pom.xml will be like below
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>YOUR_MAIN_CLASS_NAME_INCLUDING_PACKAGE</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
In the below link they didn't mentioned about the copying the jar. May be because of that you missed that plugin.
Self executing JAR
After adding the correct plugins and dependencies run to build jar
mvn package
Use below command to run in dataflow
java -jar target/<YOUR_PROJECT_ARTIFACT_ID>-<YOUR_PROJECT_VERSION>.jar \
--runner=DataflowRunner \
--project=<YOUR_GCP_PROJECT_ID> \
--region=<GCP_REGION> \
--tempLocation=gs://<YOUR_GCS_BUCKET>/temp/
To run and debug locally use DirectRunner
java -jar target/<YOUR_PROJECT_ARTIFACT_ID>-<YOUR_PROJECT_VERSION>.jar \
--runner=DirectRunner
Also pass your program arguments(eg: pipeline options)
The Cloud Dataflow dependency is not the only dependency that you'll need. You need also all the Beam SDK-related dependencies. The easiest way to obtain them is with a Maven archetype, which Beam provides:
https://beam.apache.org/get-started/quickstart-java/
mvn archetype:generate \
-DarchetypeGroupId=org.apache.beam \
-DarchetypeArtifactId=beam-sdks-java-maven-archetypes-starter \
-DarchetypeVersion=2.11.0 \
-DgroupId=org.yourorg \
-DartifactId=my-beam-pipeline \
-Dversion="0.1" \
-Dpackage=org.yourorg.pipelines \
-DinteractiveMode=false
cd my-beam-pipeline
You'll see a pom.xml file, and a directory with your starter pipeline that you can develop.
Once you have your archetype created, that'll include all the basic SDK dependencies, and you can add the Dataflow Runner as well:
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-runners-google-cloud-dataflow-java</artifactId>
<version>2.11.0</version>
</dependency>
I tried to packaged my project. But when I run the jar file, I find a bug.
Exception in thread "main" java.lang.RuntimeException: Error starting org.neo4j.kernel.impl.factory.CommunityFacadeFactory, D:\f
...
Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine#5483163c' failed to initialize. Please see attached cause exception.
...
Caused by: java.lang.IllegalArgumentException: An SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'BlockTreeOrds' does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath. The current classpath supports the following names: [Lucene50]
...
I use maven to package the project.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>db.PostgreSQL</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
The project is work when I directly run it in Intellij.
Finally, I find the solution. Using the follow maven plugin to package the project.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>main</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
This is the console output from Jenkins when I attempt to deploy my application:
Caused by: org.codehaus.cargo.util.CargoException: Cannot locate the JBoss connector classes! Make sure the required JBoss JARs (or Maven dependencies) are in CARGO's classpath.
More information on: http://cargo.codehaus.org/JBoss+Remote+Deployer
at org.codehaus.cargo.container.jboss.JBoss5xRemoteDeployer.<init> (JBoss5xRemoteDeployer.java:161)
at org.codehaus.cargo.container.jboss.JBoss7xRemoteDeployer.<init>(JBoss7xRemoteDeployer.java:41)
... 26 more
Caused by: java.lang.ClassNotFoundException: org.jboss.as.controller.client.ModelControllerClient
at jenkins.util.AntClassLoader.findClassInComponents(AntClassLoader.java:1376)
at jenkins.util.AntClassLoader.findClass(AntClassLoader.java:1326)
at jenkins.util.AntClassLoader.loadClass(AntClassLoader.java:1079)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.codehaus.cargo.container.jboss.JBoss5xRemoteDeployer.<init>(JBoss5xRemoteDeployer.java:156)
... 27 more
Build step 'Deploy war/ear to a container' marked build as failure
Finished: FAILURE
This is the content regarding CARGO in my POM file.
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<container>
<containerId>jboss7x</containerId>
<type>remote</type>
</container>
</configuration>
<executions>
<execution>
<id>deploy</id>
<phase>install</phase>
<goals>
<goal>redeploy</goal>
</goals>
<configuration>
<type>runtime</type>
<properties>
<cargo.hostname>localhost</cargo.hostname>
<cargo.jboss.management.port>19999</cargo.jboss.management.port>
</properties>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-controller-client</artifactId>
<version>7.1.1.Final</version>
</dependency>
</dependencies>
</plugin>
I've tried all the other solutions I've found online and none of them do the trick. Any ideas?
I solved this issue by copying all jar files from JBoss modules folder into the jenkins/plugins/deploy/WEB-INF.
It seems that your classloader cannot find jboss-as-controller-client, even if it is defined as plugin dependency.
You may try to define jboss-as-controller-client as project dependency. If the problem will still persist then try to add jboss-as-controller-client jar into classpath of java which is used.
I'm trying to extract some .exe files from a dependency jar file and put them under ${project.build.directory}/classes/.
But when I execute:
mvn clean compile dependency:unpack
I get:
Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.10:unpack (default-cli) on project simple: Either artifact or artifactItems is required -> [Help 1
I have verified that the dependencies are available in my local repository.
In my example pom below I've used junit as an example, but no matter which dependency I list, I get the same error.
pom.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes/externaltools</outputDirectory>
<includes>**/*.txt</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
The issue is due to you cannot use mvn clean compile dependency:unpack and <executions> tags together.
In documentation Maven Depdendency Plugin at the bottom part of the page you can read:
If you intend to configure this mojo for execution on the command line using: mvn dependency:unpack you must not put the configuration inside the executions tag. Your configuration should look like this:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[ groupId ]</groupId>
<artifactId>[ artifactId ]</artifactId>
<version>[ version ]</version>
<type>[ packaging ]</type>
<classifier> [classifier - optional] </classifier>
<overWrite>[ true or false ]</overWrite>
<outputDirectory>[ output directory ]</outputDirectory>
<destFileName>[ filename ]</destFileName>
<includes>[ comma separated list of file filters ]</includes>
<excludes>[ comma separated list of file filters ]</excludes>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
I have tried removing the <execution> tags and works perfectly!
I was trying to build ANTLR version 4 from source, as I downloaded it from the official website, but I cannot do it using ant. I downloaded the antlr-3.5-complete-no-st3.jar to the /lib folder as build.xml says, but when I run ant it returns:
[mkdir] Created dir: /../antlr/antlr4-master/build/generated-sources/antlr3/org/antlr/v4/parse
[java] error(7): cannot find or open file: *.g
BUILD FAILED
/../antlr/antlr4-master/build.xml:108: The following error occurred while executing this line:
/../antlr/antlr4-master/build.xml:84: Java returned: 1
I am on a MacBook running OSX 10.8.2
Is there anything else I have to do in order to have a successful compilation using ant?
Thanks in advance,
Dimos
You need to use Maven to build ANTLR 4 from source.
Building ANTLR 4 with Maven
Above "Building ANTLR 4 with Maven" link seems not available. Please follow the below links for ANTLR 4 maven build. These helped me to achieve antlr 4 maven build.
https://groups.google.com/forum/#!msg/antlr-discussion/Vw4Ia__sgPk/nDS5Y9YSDGIJ
How do I get help on the antlr4-maven-plugin
My ANTLR-Maven Plugin is as below:-
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0</version>
<configuration>
<sourceDirectory>${basedir}/src/main/java/com/test</sourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.antlr</groupId>
<artifactId>
antlr4-maven-plugin
</artifactId>
<versionRange>
[4.0,)
</versionRange>
<goals>
<goal>antlr4</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.2.2</version>
</dependency>
</dependencies>