Hibernate Annoation only bundle

Viewed 905
2 Answers

Disclaimer: I don't know if it is allowed by hibernate license

You can extract org.hibernate.annotations package to your final jar. You will have compile-time dependency on hibernate-core, but only org.hibernate.annotations package will be included in your final jar.

Using maven-dependency-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.8</version>
    <executions>
      <execution>
        <id>extract-hibernate-annotations</id>
        <phase>process-resources</phase>
        <goals>
          <goal>unpack</goal>
        </goals>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate-core</artifactId>
              <version>5.3.7.Final</version>
            </artifactItem>
          </artifactItems>
          <outputDirectory>${project.build.outputDirectory}</outputDirectory>
          <includes>org/hibernate/annotations/**</includes>
        </configuration>
      </execution>
    </executions>
  </plugin>

And you have to add hibernate-core as your provided dependency:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.3.7.Final</version>
  <scope>provided</scope>
</dependency>

Testing:

Main.java:

import org.hibernate.annotations.SQLDelete;

@SQLDelete(sql = "delete from whatever where id = ?")
public class Main {
  public static void main(String[] args) {
    SQLDelete annotation = Main.class.getAnnotation(SQLDelete.class);
    System.out.println("Sql delete = " + annotation);
    System.out.println("Sql = " + annotation.sql());
    try {
      Class.forName("org.hibernate.Session");
    } catch (ClassNotFoundException e) {
      System.out.println("org.hibernate.Session is not present");
    }
  }
}

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.caco3</groupId>
  <artifactId>so</artifactId>
  <version>1.0-SNAPSHOT</version>


  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.8</version>
        <executions>
          <execution>
            <id>extract-hibernate-annotations</id>
            <phase>process-resources</phase>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>org.hibernate</groupId>
                  <artifactId>hibernate-core</artifactId>
                  <version>5.3.7.Final</version>
                </artifactItem>
              </artifactItems>
              <outputDirectory>${project.build.outputDirectory}</outputDirectory>
              <includes>org/hibernate/annotations/**</includes>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>
                Main
              </mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.3.7.Final</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

</project>

Build:

$ mvn package
$ java -jar target/so-1.0-SNAPSHOT.jar

Output:

Sql delete = @org.hibernate.annotations.SQLDelete(callable=false, check=NONE, sql=delete from whatever where id = ?)
Sql = delete from whatever where id = ?
org.hibernate.Session is not present

Jar contents:

├── Main.class
├── META-INF
│   ├── MANIFEST.MF
│   └── maven
│       └── com.caco3
│           └── so
│               ├── pom.properties
│               └── pom.xml
└── org
    └── hibernate
        └── annotations
            ├── AccessType.class
            ├── Any.class
            ├── AnyMetaDef.class
            ├── AnyMetaDefs.class
            ├── AttributeAccessor.class
            ├── BatchSize.class
            ├── Cache.class
            ├── CacheConcurrencyStrategy$1.class
            ├── CacheConcurrencyStrategy.class
            ├── CacheModeType$1.class
            ├── CacheModeType.class
            ├── Cascade.class
            ├── CascadeType.class
            ├── Check.class
            ├── CollectionId.class
            ├── CollectionType.class
            ├── ColumnDefault.class
            ├── Columns.class
            ├── ColumnTransformer.class
            ├── ColumnTransformers.class
            ├── CreationTimestamp.class
            ├── DiscriminatorFormula.class
            ├── DiscriminatorOptions.class
            ├── DynamicInsert.class
            ├── DynamicUpdate.class
            ├── Entity.class
            ├── Fetch.class
            ├── FetchMode.class
            ├── FetchProfile$FetchOverride.class
            ├── FetchProfile.class
            ├── FetchProfiles.class
            ├── Filter.class
            ├── FilterDef.class
            ├── FilterDefs.class
            ├── FilterJoinTable.class
            ├── FilterJoinTables.class
            ├── Filters.class
            ├── FlushModeType.class
            ├── ForeignKey.class
            ├── Formula.class
            ├── Generated.class
            ├── GenerationTime.class
            ├── GeneratorType.class
            ├── GenericGenerator.class
            ├── GenericGenerators.class
            ├── Immutable.class
            ├── Index.class
            ├── IndexColumn.class
            ├── JoinColumnOrFormula.class
            ├── JoinColumnsOrFormulas.class
            ├── JoinFormula.class
            ├── LazyCollection.class
            ├── LazyCollectionOption.class
            ├── LazyGroup.class
            ├── LazyToOne.class
            ├── LazyToOneOption.class
            ├── ListIndexBase.class
            ├── Loader.class
            ├── ManyToAny.class
            ├── MapKeyType.class
            ├── MetaValue.class
            ├── NamedNativeQueries.class
            ├── NamedNativeQuery.class
            ├── NamedQueries.class
            ├── NamedQuery.class
            ├── Nationalized.class
            ├── NaturalIdCache.class
            ├── NaturalId.class
            ├── NotFoundAction.class
            ├── NotFound.class
            ├── OnDeleteAction.class
            ├── OnDelete.class
            ├── OptimisticLock.class
            ├── OptimisticLocking.class
            ├── OptimisticLockType.class
            ├── OrderBy.class
            ├── ParamDef.class
            ├── Parameter.class
            ├── Parent.class
            ├── Persister.class
            ├── Polymorphism.class
            ├── PolymorphismType.class
            ├── Proxy.class
            ├── QueryHints.class
            ├── ResultCheckStyle.class
            ├── RowId.class
            ├── SelectBeforeUpdate.class
            ├── Sort.class
            ├── SortComparator.class
            ├── SortNatural.class
            ├── SortType.class
            ├── Source.class
            ├── SourceType.class
            ├── SQLDeleteAll.class
            ├── SQLDelete.class
            ├── SqlFragmentAlias.class
            ├── SQLInsert.class
            ├── SQLUpdate.class
            ├── Subselect.class
            ├── Synchronize.class
            ├── Table.class
            ├── Tables.class
            ├── Target.class
            ├── Tuplizer.class
            ├── Tuplizers.class
            ├── Type.class
            ├── TypeDef.class
            ├── TypeDefs.class
            ├── UpdateTimestamp.class
            ├── ValueGenerationType.class
            ├── Where.class
            └── WhereJoinTable.class

I don't know hibernate jar that will contain only clean annotations including @Type and nothing else hibernate related.

I am try to guess, you want to get a jar that will contains code that uses @Type annotation in import, but does not depend on hibernate-core.

Use provided maven scope for this case.

Google says

Maven dependency scope – provided. Maven dependency scope provided is used during build and test the project. They are also required to run, but should not exported, because the dependency will be provided by the runtime, for instance, by servlet container or application server.

What means 'requied to run' and 'will be provided' here is that classpath have to load

org.hibernate.annotations.Type 

From anywhere, most probably from existed hibernate-core jar, but in fact it is not necessary to be exactly hibernate-core. Java class coordinates is class_name + package + classloader. There is some considerations on special packages names like java.lang, but not for org.hibernate.* if you will create your own jar, that will contain this same package + annotation with same name, effect will be the same.

In other words you can create your own @Type annotation itself in your own jar if you strictly don't want it from hibernate-core.

Hope this will help.

Related