Unable to configure maven dependencies correctly - maven

I have a web-app with following dependencies in pom:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.9.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
<scope>test</scope>
</dependency>
With this configuration, I get following error in junit test:
java.lang.ClassFormatError: Absent Code attribute in method that is
not native or abstract in class file javax/persistence/Persistence
I found some info on this error here.
So, I removed 'javaee-web-api' dependency.
After that I get compilation error 'package javax.persistence does not exist', and many more.
So, I changed scope of 'hibernate-entitymanager' to default (compile).
The working pom (dependencies) look like this:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.9.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
</dependency>
Now everything is OK, except generated war contains hibernate jar, which I do not want.
Any suggestion how to fix this, correct maven pom?

If you are working with an in application server, you can replace the hibernate dependency with a (still vendor specific) JPA API with provided scope like this ( example is for JBoss with hibernate):
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<scope>provided</scope>
</dependency>

Finally, I got this working, with same set of dependencies, using profiles.
Here is relevant section of pom:
<profiles>
<profile>
<id>test</id>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>package</id>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.9.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Now, I use following commands to package and test,
mvn -Ptest test
mvn -Ppackage package -DskipTests
-DskipTests is required, as, otherwise, package goal triggers 'test' causing the same error I started with, as it uses same set of dependencies configured for 'package'.
java.lang.ClassFormatError: Absent Code attribute in method that is
not native or abstract in class file javax/persistence/Persistence

I was having the same problem and was able to solve it by adding a couple of dependencies to the Maven pom.xml file. See this answer for more details.

Related

Failed to load driver class oracle.jdbc.OracleDriver in either of HikariConfig class loader or Thread context

I'm working with Spring for the first time and trying to set it up so that my code can access an Oracle database. I have the following configuration in my application.properties:
spring.datasource.url=jdbc:oracle:thin:#140.192.30.237:1521:def
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver.class-name=oracle.jdbc.driver.OracleDriver
My pom.xml contains the following dependencies:
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Yet I get the following error and am not sure how to solve it, I"ve found similar ones from searching but none that have solved my problem:
Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:
Property: driverclassname
Value: oracle.jdbc.OracleDriver
Origin: "driverClassName" from property source "source"
Reason: Failed to load driver class oracle.jdbc.OracleDriver in either of HikariConfig class loader or Thread context classloader
Action:
Update your application's configuration
Thanks for any tips to solve this.
As its mentioned in your error the problem is with your configuration.
Following line,
spring.datasource.driver.class-name=oracle.jdbc.driver.OracleDriver
should change as,
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
Note that its not driver.class-name, instead its driver-class-name
Add below dependency and repository in the pom
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<repositories>
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
</repositories>
Also add the following properties in the application.properties
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:#localhost:1521:xe(SID)
spring.datasource.username=system
spring.datasource.password=pw
Try on the project directory pom.xml file in in command promt or your ide.
mvn clean install
I have the same probleme
Add this dependence to POM.xml file
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
Below dependency to POM should work.
<!-- https://mvnrepository.com/artifact/oracle/ojdbc6 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>

Maven Dependencies : non existing library 'hadoop-common-2.6.0-cdh5.9.0.jar'

I am trying to implement an Spark app WordCount, but i get an error when I add hadoop-common dependency:
'Maven Dependencies' references non existing library '/root/.m2/repository/org/apache/hadoop/hadoop-common/2.6.0-cdh5.9.0/hadoop-common-2.6.0-cdh5.9.0.jar'
and this my pom.xml :
<dependencies>
<!-- https://mvnrepository.com/artifact/org.scala-lang/scala-library -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.6</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0-cdh5.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
my haddop version is 2.6.0-cdh5.9.0.
Any help please!!
replace
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0-cdh5.9.0</version>
</dependency>
with
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
or use external jar and add http://repo.spring.io/libs-milestone/org/apache/hadoop/hadoop-common/2.6.0-cdh5.9.0/hadoop-common-2.6.0-cdh5.9.0.jar
hope this helps

spark 2.0.2 ClassNotFoundException: org.apache.kafka.clients.consumer.Consumer

Below is my pom.xml. I build the jar with maven shade. I am very sure with the org.apache.kafka.clients.consumer.Consumer is included in my uber jar. Also I have put the kafka-clients-0.10.1.0.jar into spark spark.driver.extraLibraryPath. I also tried add --jars option in spark-submit command. But I still get the classNotFoundException.
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>2.11.8</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.11</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka-0-10_2.11</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.11</artifactId>
<version>0.10.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
I just find a bypass solution. Add the jar into SPARK_HOME/jars.
I use spark-submit command. Tried add --jars,--driver-library-path. I am sure the options take effect. But still classNotFound.
I find the bypass solution according to the driver log listed as below.
enter image description here
Basically, you need:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.1.0</version>
</dependency>

Selenium strange exception in intellij idea

I get this exception when I want to run my selenium tests in intellij:
geb.driver.DriverCreationException: failed to create driver from callback 'script14601033971711755627056$_run_closure5#2eb91a50'
at geb.driver.CallbackDriverFactory.getDriver(CallbackDriverFactory.groovy:35)
at geb.driver.CachingDriverFactory.getDriver_closure3(CachingDriverFactory.groovy:85)
at geb.driver.CachingDriverFactory.getDriver_closure3(CachingDriverFactory.groovy)
at geb.driver.CachingDriverFactory$SimpleCache.get(CachingDriverFactory.groovy:32)
at geb.driver.CachingDriverFactory.getDriver(CachingDriverFactory.groovy:84)
at geb.Configuration.createDriver(Configuration.groovy:382)
at geb.Configuration.getDriver(Configuration.groovy:371)
at geb.Browser.getDriver(Browser.groovy:109)
at geb.spock.GebSpec.propertyMissing(GebSpec.groovy:60)
at specs.LomnidoBaseSpec.setupSpec(LomnidoBaseSpec.groovy:28)
Caused by: java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
at org.openqa.selenium.remote.internal.HttpClientFactory.getClientConnectionManager(HttpClientFactory.java:71)
at org.openqa.selenium.remote.internal.HttpClientFactory.<init>(HttpClientFactory.java:57)
at org.openqa.selenium.remote.internal.HttpClientFactory.<init>(HttpClientFactory.java:60)
at org.openqa.selenium.remote.internal.ApacheHttpClient$Factory.getDefaultHttpClientFactory(ApacheHttpClient.java:252)
at org.openqa.selenium.remote.internal.ApacheHttpClient$Factory.<init>(ApacheHttpClient.java:229)
at org.openqa.selenium.remote.HttpCommandExecutor.getDefaultClientFactory(HttpCommandExecutor.java:96)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:70)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:58)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:87)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:119)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:216)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
at script14601033971711755627056.run_closure5(script14601033971711755627056.groovy:81)
at script14601033971711755627056.run_closure5(script14601033971711755627056.groovy)
at geb.driver.CallbackDriverFactory.getDriver(CallbackDriverFactory.groovy:29)
... 9 more
When I run the test from command line with maven, everything works fine.
mvn -Dtest=specs.writeable.CreateDemoWorkflow test
My pom file:
<properties>
<gebVersion>0.12.2</gebVersion>
<seleniumVersion>2.53.0</seleniumVersion>
<groovyVersion>2.4.3</groovyVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovyVersion}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.gebish</groupId>
<artifactId>geb-spock</artifactId>
<version>${gebVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>${seleniumVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>${seleniumVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.20</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>sauce_junit</artifactId>
<version>2.1.21</version>
<scope>test</scope>
</dependency>
The trouble started after I go from version 2.48.2 to 2.53.0. Before everything worked fine. But I can not use the old version, the error stays.
When I delete all files in m2/repository/org/apache/httpcomponents, the tests can start. But when I run my grails application afterwards, older httpcomponents are downloaded and my test do not run any longer. In my project structure just the new jars are included
I found the solution:
From other projects, there was this maven repository:
/.m2/repository/org/apache/httpcomponents/httpcore/4.2.2
After deleting this folder, everything works fine. So I have to update the other project to use a new version

The exclusion tag can't solve the duplicated dependency version

Here is my problem, I have a parent pom.xml which defines the:
--------parent pom.xml-----------------
<dependencyManagement>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>robolectric</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.0.3</version>
</dependency>
</dependencyManagement>
Inside this roboletric pom.xml we have:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-ant-tasks</artifactId>
<version>2.1.3</version>
</dependency>
Inside the maven-ant-tasks pom.xml, it has:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.2.1</version>
</dependency>
So whenever I am calling the API which only exists in the maven-artifact 3.0.3 in the roboletric test case, it will throw nosuchmethod exception.
So I have tried:
1. <dependency>
<groupId>org.robolectric</groupId>
<artifactId>robolectric</artifactId>
<version>2.3</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
</exclusion>
</exclusions>
</dependency>
I also tried moving:
org.apache.maven
maven-artifact
3.0.3
to the child pom under the <dependencyManagement> which I think it should override the parent but actually it didn't.
I think this kind of problem should be very common but I just can't figure it out, any suggestions?
You should define dependency management in your parent POM to nail down the version of maven-artifact in all (transitive) dependencies:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
</dependencyManagement>

Resources