1

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?

Atul Acharya
  • 497
  • 2
  • 8
  • 21

3 Answers3

0

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>
kostja
  • 60,521
  • 48
  • 179
  • 224
  • thanks @kostja, this did work. but I'm not targettin any specific application server. – Atul Acharya Feb 09 '13 at 12:26
  • @Atul I'm afraid there is no common dependency for both eclipselink and hibernate. See [this](http://stackoverflow.com/questions/6836772/where-can-i-find-a-jpa2-maven-dependency) and [this](http://stackoverflow.com/questions/2243147/jar-file-for-jpa-2-0) question. You will have to decide depending on the implementation used by the application server. – kostja Feb 09 '13 at 16:21
  • hmm, too bad. Wish there was something cleaner. – Atul Acharya Feb 10 '13 at 05:38
  • @Atul yeah, it's strange that despite the standard and at least two standard implementations there is still no common API depencency. Maybe for JPA 2.1 then :) – kostja Feb 10 '13 at 07:27
0

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

Atul Acharya
  • 497
  • 2
  • 8
  • 21
0

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.

Community
  • 1
  • 1
A. Rick
  • 649
  • 5
  • 11