HHH-8485 - Apply newly extracted version-injection plugin
This commit is contained in:
parent
08ab72dc5c
commit
cbd828217a
|
@ -23,6 +23,7 @@ buildscript {
|
|||
dependencies {
|
||||
classpath 'org.hibernate.build.gradle:gradle-maven-publish-auth:2.0.1'
|
||||
classpath 'org.hibernate.build.gradle:hibernate-matrix-testing:1.0.0-SNAPSHOT'
|
||||
classpath 'org.hibernate.build.gradle:version-injection-plugin:1.0.0-SNAPSHOT'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
apply plugin: 'groovy'
|
||||
apply plugin: 'idea'
|
||||
|
||||
buildDir = "target"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
mavenLocal()
|
||||
mavenRepo name: "jboss", url: "http://repository.jboss.org/nexus/content/groups/public/"
|
||||
}
|
||||
targetCompatibility = "1.6"
|
||||
sourceCompatibility = "1.6"
|
||||
|
||||
dependencies {
|
||||
// common
|
||||
compile gradleApi()
|
||||
compile localGroovy()
|
||||
compile 'org.apache.ant:ant:1.8.2'
|
||||
// injection plugin
|
||||
compile 'org.javassist:javassist:3.15.0-GA'
|
||||
|
||||
|
||||
}
|
||||
|
||||
idea {
|
||||
project {
|
||||
languageLevel = '1.6'
|
||||
}
|
||||
module {
|
||||
downloadSources = true
|
||||
downloadJavadoc = true
|
||||
}
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.build.gradle.inject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class Injection {
|
||||
private final String expression;
|
||||
private List<TargetMember> targetMembers = new ArrayList<TargetMember>();
|
||||
|
||||
public Injection(String expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public String getExpression() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
public void into(String className, String member) {
|
||||
into( new TargetMember( className, member ) );
|
||||
}
|
||||
|
||||
public void into(TargetMember targetMember) {
|
||||
targetMembers.add( targetMember );
|
||||
}
|
||||
|
||||
public Iterable<TargetMember> getTargetMembers() {
|
||||
return targetMembers;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,253 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.build.gradle.inject;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javassist.ClassPool;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtField;
|
||||
import javassist.CtMethod;
|
||||
import javassist.LoaderClassPath;
|
||||
import javassist.Modifier;
|
||||
import javassist.NotFoundException;
|
||||
import javassist.bytecode.ClassFile;
|
||||
import javassist.bytecode.ConstantAttribute;
|
||||
import javassist.bytecode.FieldInfo;
|
||||
import org.gradle.api.Action;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.Task;
|
||||
import org.gradle.api.plugins.JavaPluginConvention;
|
||||
import org.gradle.api.tasks.SourceSet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class InjectionAction implements Action<Task> {
|
||||
private static final Logger log = LoggerFactory.getLogger( InjectionAction.class );
|
||||
|
||||
private final Project project;
|
||||
|
||||
private List<Injection> injections = new ArrayList<Injection>();
|
||||
|
||||
private LoaderClassPath loaderClassPath;
|
||||
private ClassPool classPool;
|
||||
|
||||
public InjectionAction(Project project) {
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
void addInjection(Injection injection) {
|
||||
injections.add( injection );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Task task) {
|
||||
final ClassLoader runtimeScopeClassLoader = buildRuntimeScopeClassLoader();
|
||||
|
||||
loaderClassPath = new LoaderClassPath( runtimeScopeClassLoader );
|
||||
classPool = new ClassPool( true );
|
||||
classPool.appendClassPath( loaderClassPath );
|
||||
|
||||
try {
|
||||
performInjections();
|
||||
}
|
||||
finally {
|
||||
loaderClassPath.close();
|
||||
}
|
||||
}
|
||||
|
||||
private ClassLoader buildRuntimeScopeClassLoader() {
|
||||
final ArrayList<URL> classPathUrls = new ArrayList<URL>();
|
||||
final SourceSet mainSourceSet = project
|
||||
.getConvention()
|
||||
.getPlugin( JavaPluginConvention.class )
|
||||
.getSourceSets()
|
||||
.findByName( SourceSet.MAIN_SOURCE_SET_NAME );
|
||||
for ( File file : mainSourceSet.getRuntimeClasspath() ) {
|
||||
try {
|
||||
classPathUrls.add( file.toURI().toURL() );
|
||||
}
|
||||
catch (MalformedURLException e) {
|
||||
throw new InjectionException( "Could not determine artifact URL [" + file.getPath() + "]", e );
|
||||
}
|
||||
}
|
||||
return new URLClassLoader( classPathUrls.toArray( new URL[classPathUrls.size()] ), getClass().getClassLoader() );
|
||||
}
|
||||
|
||||
private void performInjections() {
|
||||
for ( Injection injection : injections ) {
|
||||
for ( TargetMember targetMember : injection.getTargetMembers() ) {
|
||||
resolveInjectionTarget( targetMember ).inject( injection.getExpression() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private InjectionTarget resolveInjectionTarget(TargetMember targetMember) {
|
||||
try {
|
||||
final CtClass ctClass = classPool.get( targetMember.getClassName() );
|
||||
// see if it is a field...
|
||||
try {
|
||||
CtField field = ctClass.getField( targetMember.getMemberName() );
|
||||
return new FieldInjectionTarget( targetMember, ctClass, field );
|
||||
}
|
||||
catch( NotFoundException ignore ) {
|
||||
}
|
||||
|
||||
// see if it is a method...
|
||||
for ( CtMethod method : ctClass.getMethods() ) {
|
||||
if ( method.getName().equals( targetMember.getMemberName() ) ) {
|
||||
return new MethodInjectionTarget( targetMember, ctClass, method );
|
||||
}
|
||||
}
|
||||
|
||||
// finally throw an exception
|
||||
throw new InjectionException( "Unknown member [" + targetMember.getQualifiedName() + "]" );
|
||||
}
|
||||
catch ( Throwable e ) {
|
||||
throw new InjectionException( "Unable to resolve class [" + targetMember.getClassName() + "]", e );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Strategy for performing an injection
|
||||
*/
|
||||
private static interface InjectionTarget {
|
||||
/**
|
||||
* Inject the given value per this target's strategy.
|
||||
*
|
||||
* @param value The value to inject.
|
||||
*
|
||||
* @throws org.hibernate.build.gradle.inject.InjectionException Indicates a problem performing the injection.
|
||||
*/
|
||||
public void inject(String value);
|
||||
}
|
||||
|
||||
private abstract class BaseInjectionTarget implements InjectionTarget {
|
||||
@SuppressWarnings( {"UnusedDeclaration"})
|
||||
private final TargetMember targetMember;
|
||||
private final CtClass ctClass;
|
||||
private final File classFileLocation;
|
||||
|
||||
protected BaseInjectionTarget(TargetMember targetMember, CtClass ctClass) {
|
||||
this.targetMember = targetMember;
|
||||
this.ctClass = ctClass;
|
||||
try {
|
||||
classFileLocation = new File( loaderClassPath.find( targetMember.getClassName() ).toURI() );
|
||||
}
|
||||
catch ( Throwable e ) {
|
||||
throw new InjectionException( "Unable to resolve class file path", e );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inject(String value) {
|
||||
doInjection( value );
|
||||
writeOutChanges();
|
||||
}
|
||||
|
||||
protected abstract void doInjection(String value);
|
||||
|
||||
protected void writeOutChanges() {
|
||||
log.info( "writing injection changes back [" + classFileLocation.getAbsolutePath() + "]" );
|
||||
long timeStamp = classFileLocation.lastModified();
|
||||
ClassFile classFile = ctClass.getClassFile();
|
||||
classFile.compact();
|
||||
try {
|
||||
DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( classFileLocation ) ) );
|
||||
try {
|
||||
|
||||
classFile.write( out );
|
||||
out.flush();
|
||||
if ( ! classFileLocation.setLastModified( System.currentTimeMillis() ) ) {
|
||||
log.info( "Unable to manually update class file timestamp" );
|
||||
}
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
classFileLocation.setLastModified( timeStamp );
|
||||
}
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
throw new InjectionException( "Unable to write out modified class file", e );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class FieldInjectionTarget extends BaseInjectionTarget {
|
||||
private final CtField ctField;
|
||||
|
||||
private FieldInjectionTarget(TargetMember targetMember, CtClass ctClass, CtField ctField) {
|
||||
super( targetMember, ctClass );
|
||||
this.ctField = ctField;
|
||||
if ( ! Modifier.isStatic( ctField.getModifiers() ) ) {
|
||||
throw new InjectionException( "Field is not static [" + targetMember.getQualifiedName() + "]" );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doInjection(String value) {
|
||||
final FieldInfo ctFieldInfo = ctField.getFieldInfo();
|
||||
|
||||
ctFieldInfo.addAttribute(
|
||||
new ConstantAttribute(
|
||||
ctFieldInfo.getConstPool(),
|
||||
ctFieldInfo.getConstPool().addStringInfo( value )
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private class MethodInjectionTarget extends BaseInjectionTarget {
|
||||
private final CtMethod ctMethod;
|
||||
|
||||
private MethodInjectionTarget(TargetMember targetMember, CtClass ctClass, CtMethod ctMethod) {
|
||||
super( targetMember, ctClass );
|
||||
this.ctMethod = ctMethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doInjection(String value) {
|
||||
try {
|
||||
ctMethod.setBody( "{return \"" + value + "\";}" );
|
||||
}
|
||||
catch ( Throwable t ) {
|
||||
throw new InjectionException( "Unable to replace method body", t );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.build.gradle.inject;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class InjectionException extends RuntimeException {
|
||||
public InjectionException(String message) {
|
||||
super( message );
|
||||
}
|
||||
|
||||
public InjectionException(String message, Throwable cause) {
|
||||
super( message, cause );
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.build.gradle.inject;
|
||||
|
||||
import org.gradle.api.Plugin;
|
||||
import org.gradle.api.Project;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class InjectionPlugin implements Plugin<Project> {
|
||||
|
||||
@Override
|
||||
public void apply(Project project) {
|
||||
final InjectionAction injectionAction = new InjectionAction( project );
|
||||
|
||||
// For now, just do what Hibernate needs rather than a full blown "convention" object
|
||||
Injection versionInjection = new Injection( project.getVersion().toString() );
|
||||
versionInjection.into( "org.hibernate.Version", "getVersionString" );
|
||||
injectionAction.addInjection( versionInjection );
|
||||
project.getTasks().findByName( "compileJava" ).doLast( injectionAction );
|
||||
}
|
||||
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.build.gradle.inject;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class TargetMember {
|
||||
private final String className;
|
||||
private final String memberName;
|
||||
|
||||
public TargetMember(String className, String memberName) {
|
||||
this.className = className;
|
||||
this.memberName = memberName;
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
return className;
|
||||
}
|
||||
|
||||
public String getMemberName() {
|
||||
return memberName;
|
||||
}
|
||||
|
||||
public String getQualifiedName() {
|
||||
return getClassName() + "#" + getMemberName();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.build.gradle.util;
|
||||
|
||||
/**
|
||||
* And exception intended to fail the build.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class BuildException extends RuntimeException {
|
||||
public BuildException(String message) {
|
||||
super( message );
|
||||
}
|
||||
|
||||
public BuildException(String message, Throwable cause) {
|
||||
super( message, cause );
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.build.gradle.util;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @author Strong Liu
|
||||
*/
|
||||
public class FileUtil {
|
||||
private FileUtil() {
|
||||
}
|
||||
|
||||
public static boolean isFile(File file) {
|
||||
return file != null && file.exists() && file.isFile();
|
||||
}
|
||||
|
||||
public static void mkdir(File file) {
|
||||
if ( file == null || file.exists() ) {
|
||||
return;
|
||||
}
|
||||
if ( !file.getParentFile().exists() ) {
|
||||
mkdir( file.getParentFile() );
|
||||
}
|
||||
file.mkdir();
|
||||
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.build.gradle.util;
|
||||
|
||||
/**
|
||||
* TODO : javadoc
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class JavaVersion {
|
||||
public static enum Family {
|
||||
JAVA7( 7 ),
|
||||
JAVA6( 6 ),
|
||||
JAVA5( 5 );
|
||||
|
||||
private final int code;
|
||||
|
||||
private Family(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
private final String fullVersionString;
|
||||
private final Family family;
|
||||
|
||||
public JavaVersion(String javaVersionString) {
|
||||
this.fullVersionString = javaVersionString;
|
||||
family = fullVersionString.startsWith( "1.6" )
|
||||
? Family.JAVA6
|
||||
: ( fullVersionString.startsWith( "1.7" ) ? Family.JAVA7 : Family.JAVA5);
|
||||
}
|
||||
|
||||
public String getFullVersionString() {
|
||||
return fullVersionString;
|
||||
}
|
||||
|
||||
public Family getFamily() {
|
||||
return family;
|
||||
}
|
||||
|
||||
public boolean isAtLeast(Family family) {
|
||||
return getFamily().code >= family.code;
|
||||
}
|
||||
}
|
|
@ -1,165 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.hibernate.build.gradle.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.apache.tools.ant.taskdefs.condition.Os;
|
||||
import org.apache.tools.ant.util.FileUtils;
|
||||
|
||||
/**
|
||||
* Models path information for a particular JDK install.
|
||||
* <p/>
|
||||
* Copied largely from {@link org.gradle.util.Jvm} and {@link org.apache.tools.ant.util.JavaEnvUtils}. The main
|
||||
* difference is that those classes are static, based solely on the reported "java.home" sys prop. Also, Ant's
|
||||
* JavaEnvUtils allows for use of either a JRE or JDK; we do not care about allowing for a JRE-only set up here.
|
||||
*
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class Jdk {
|
||||
private static final boolean IS_DOS = Os.isFamily( "dos" );
|
||||
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
|
||||
|
||||
private final File jdkHome;
|
||||
private final JavaVersion version;
|
||||
public Jdk(){
|
||||
this(System.getenv( "JAVA_HOME" ));
|
||||
}
|
||||
|
||||
public Jdk(File jdkHome) {
|
||||
this.jdkHome = jdkHome;
|
||||
if ( !jdkHome.exists() ) {
|
||||
throw new IllegalArgumentException( "Invalid path specified for JDK home; " + jdkHome.getAbsolutePath() + " did not exist" );
|
||||
}
|
||||
this.version = determineJdkVersion();
|
||||
}
|
||||
|
||||
public Jdk(String jdkHomePath) {
|
||||
this( new File( jdkHomePath ) );
|
||||
}
|
||||
|
||||
public File getJavaExecutable() {
|
||||
return new File( getJdkExecutable( "java" ) );
|
||||
}
|
||||
|
||||
public File getJavacExecutable() {
|
||||
return new File( getJdkExecutable( "javac" ) );
|
||||
}
|
||||
|
||||
public File getJavadocExecutable() {
|
||||
return new File( getJdkExecutable( "javadoc" ) );
|
||||
}
|
||||
|
||||
public JavaVersion getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
protected String getJdkExecutable(String command) {
|
||||
File executable = findInDir( jdkHome + "/bin", command );
|
||||
|
||||
if ( executable == null ) {
|
||||
executable = findInDir( jdkHome + "/../bin", command );
|
||||
}
|
||||
|
||||
if ( executable != null ) {
|
||||
return executable.getAbsolutePath();
|
||||
}
|
||||
else {
|
||||
// Unfortunately on Windows java.home doesn't always refer
|
||||
// to the correct location, so we need to fall back to
|
||||
// assuming java is somewhere on the PATH.
|
||||
return addExtension( command );
|
||||
}
|
||||
}
|
||||
|
||||
private static File findInDir(String dirName, String commandName) {
|
||||
File dir = FILE_UTILS.normalize(dirName);
|
||||
File executable = null;
|
||||
if (dir.exists()) {
|
||||
executable = new File(dir, addExtension(commandName));
|
||||
if (!executable.exists()) {
|
||||
executable = null;
|
||||
}
|
||||
}
|
||||
return executable;
|
||||
}
|
||||
|
||||
private static String addExtension(String command) {
|
||||
// This is the most common extension case - exe for windows and OS/2,
|
||||
// nothing for *nix.
|
||||
return command + (IS_DOS ? ".exe" : "");
|
||||
}
|
||||
|
||||
private JavaVersion determineJdkVersion() {
|
||||
String javaVersionString = extractFromSunJdk();
|
||||
if ( javaVersionString == null ) {
|
||||
javaVersionString = "1.6";//make 1.6 as default
|
||||
}
|
||||
return new JavaVersion( javaVersionString );
|
||||
}
|
||||
|
||||
private String extractFromSunJdk() {
|
||||
String version = null;
|
||||
|
||||
try {
|
||||
final File javaCommand = getJavaExecutable();
|
||||
|
||||
// Fix build for e.g. windows when path to java command contains spaces
|
||||
// Using the array for Runtime.exec will make sure that arguments with spaces get quoted
|
||||
Process javaProcess = Runtime.getRuntime().exec( new String[]{javaCommand.getAbsolutePath(), "-version"} );
|
||||
|
||||
try {
|
||||
version = extractVersion( new BufferedReader( new InputStreamReader( javaProcess.getErrorStream() ) ) );
|
||||
if( version == null || version.equals( "" )){
|
||||
version = extractVersion( new BufferedReader( new InputStreamReader( javaProcess.getInputStream() ) ) );
|
||||
}
|
||||
}
|
||||
finally {
|
||||
javaProcess.destroy();
|
||||
}
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
throw new RuntimeException( "Unable to determine Java version", e );
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
private String extractVersion(BufferedReader br) throws IOException{
|
||||
final String key = "version \"";
|
||||
String line = null;
|
||||
String version = null;
|
||||
while ( (line = br.readLine()) != null) {
|
||||
if ( version == null && line.contains( key ) ) {
|
||||
version = line.substring( line.indexOf( key ) + key.length(), line.length() - 1 );
|
||||
}
|
||||
}
|
||||
br.close();
|
||||
return version;
|
||||
}
|
||||
}
|
|
@ -1,8 +1,11 @@
|
|||
apply plugin: 'antlr'
|
||||
apply plugin: 'hibernate-matrix-testing'
|
||||
|
||||
apply plugin: org.hibernate.build.gradle.inject.InjectionPlugin
|
||||
apply plugin: 'version-injection'
|
||||
|
||||
versionInjection {
|
||||
into( 'org.hibernate.Version', 'getVersionString' )
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile( libraries.jta )
|
||||
|
@ -46,12 +49,10 @@ def pomDescription() {
|
|||
return 'The core O/RM functionality as provided by Hibernate'
|
||||
}
|
||||
|
||||
manifest.mainAttributes(
|
||||
'Main-Class': 'org.hibernate.Version'
|
||||
)
|
||||
|
||||
jar {
|
||||
manifest {
|
||||
mainAttributes( 'Main-Class': 'org.hibernate.Version' )
|
||||
|
||||
instruction 'Bundle-Description', 'Hibernate ORM Core'
|
||||
|
||||
instructionFirst 'Import-Package',
|
||||
|
|
Loading…
Reference in New Issue