최대 절전 모드 용 Maven Java 소스 코드 생성
문제
기존 프로젝트를 개미 빌드에서 Maven을 사용하는 프로젝트로 변환하는 데 바쁩니다. 이 빌드의 일부에는 최대 절전 모드 HBM2Java 도구를 사용하여 .hbm.xml 파일 모음을 Java로 변환하는 것이 포함됩니다. 다음은 이것을 수행하는 데 사용되는 개미 스크립트의 스 니펫입니다.
<target name="dbcodegen" depends="cleangen"
description="Generate Java source from Hibernate XML">
<hibernatetool destdir="${src.generated}">
<configuration>
<fileset dir="${src.config}">
<include name="**/*.hbm.xml"/>
</fileset>
</configuration>
<hbm2java jdk5="true"/>
</hibernatetool>
</target>
나는 인터넷을 둘러 보았고 일부 사람들은 Maven 내에서 Ant와 Maven 플러그인을 사용하여 이것을하는 것 같습니다 (나는 생각합니다. 개미와 Maven을 혼합하지 않기를 원합니다. 모든 .hbm.xml 파일이 수거되고 코드 생성이 Maven 코드 생성 빌드 단계의 일부로 진행되도록이를 수행하는 방법을 제안 할 수 있습니까?
감사!
아담.
해결책
글쎄, Maven Hibernate3 플러그인 Ant와 Maven을 혼합하고 싶지 않다면 (여기에서 좋은 생각입니다). 그것은 있습니다 hbm2java
기본적으로 묶인 목표 generate-sources
단계. 자세한 내용은 Mojo 웹 사이트를 참조하지만 플러그인 설정은 다음과 같은 것처럼 보일 수 있습니다.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>hbm2java</goal>
</goals>
</execution>
</executions>
<configuration>
<components>
<component>
<name>hbm2java</name>
<implementation>configuration</implementation>
<outputDirectory>target/generated-sources/hibernate3</outputDirectory>
</component>
</components>
<componentProperties>
<drop>true</drop>
<jdk5>true</jdk5>
<configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile>
</componentProperties>
</configuration>
</plugin>
편집하다: 플러그인이 실제로 찾고 있습니다 .hbm.xml
안에 target/classes
Java 소스 파일을 생성합니다. 따라서 매핑 파일을 넣으면 src/main/resources
, 그들은 복사됩니다 target/classes
시 process-resources
플러그인에 의해 호출되는 단계는 단지 작동합니다. 방금 다음 샘플 프로젝트로 테스트했습니다.
maven-hibernate3-testcase |-- pom.xml `-- src |-- main | |-- java | `-- resources | |-- Person.hbm.xml | `-- hibernate.cfg.xml `-- test `-- java
그만큼 pom.xml
거의 비어 있습니다. 위에 표시된 플러그인 구성과 주니트 종속성 만 포함되어 있습니다. 그만큼 hibernate.cfg.xml
포함
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="connection.url">jdbc:derby://localhost:1527/mydatabase</property>
<property name="connection.username">app</property>
<property name="connection.password">app</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Mapping files -->
<mapping resource="Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
그리고 Person.hbm.xml
다음과 같이 보입니다.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Person" table="person">
<id name="id" type="int">
<generator class="increment" />
</id>
<property name="name" column="cname" type="string" />
</class>
</hibernate-mapping>
이 구성으로 실행 중입니다 mvn install
생성합니다 Person.java
아래 그림과 같이:
$ cat target/generated-sources/hibernate3/Person.java
// default package
// Generated Dec 14, 2009 2:19:22 PM by Hibernate Tools 3.2.2.GA
/**
* Person generated by hbm2java
*/
public class Person implements java.io.Serializable {
private int id;
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
모든 것이 설명 된대로 작동합니다.
다른 팁
파스칼, 도와 주셔서 다시 한 번 감사드립니다! 솔루션이 잘 작동합니다.
이 작업을 수행하는 동안 내가 만난 몇 가지 다른 것들. 첫 번째는 이것이 상당히 큰 프로젝트라는 사실과 관련이 있으므로 여러 Maven 모듈로 분할하여 원래 Ant Multi-Directory 빌드를 반영합니다. 생성 된 클래스가 포함 된 모듈은 실제로 데이터베이스 액세스를 수행하지 않으므로 Hibernate.cfg.xml 파일에는 필요하지 않으므로 DB 연결 정보를 포함하지 않아야합니다. 나는 이것을 시도했고 그것은 모든 속성 태그가 제거 된 Pascal이 제공 한 파일의 컷 다운 버전으로 잘 작동합니다.
이것으로 빌드는 명령 줄에서 잘 작동했습니다. 그러나 내가 시도해 볼 때 시도해 보면, Eclipse에서 실행될 때 다른 모듈을 설득 할 수 없었습니다. 당분간, 내가해야 할 솔루션은 구성/구성 요소/구성 요소에서 다음 줄을 POM에 추가하는 것입니다.
<outputDirectory>/src/main/java</outputDirectory>
이로 인해 Eclipse가 다른 모듈을 위해 픽업 할 수있는 장소에서 파일이 생성됩니다. 이 작업이 완료되면 명령 줄을 빌드 한 다음 Eclipse가 소스 디렉토리의 내용을 새로 고치도록 요청해야합니다. 아직, 나는 이것을 처리하는 더 깨끗한 방법을 모른다 ....
위상 컴파일에 *.hbm.xml 포함이 필요한 경우; pom.xml 편집하고 다음 코드를 추가하십시오.
<build>
<resources>
<resource>
<directory>source/com/qfund/orm/</directory>
<targetPath>com/qfund/orm/</targetPath>
<includes>
<include>*.hbm.xml</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/java/</directory>
<includes>
<include>*.xml</include>
<include>*.xsd</include>
<include>*.xslt</include>
<include>*.properties</include>
</includes>
</testResource>
</testResources>
</build>