将基于ANT的NetBeans项目转换为Gradle项目?

将基于ANT的NetBeans项目转换为Gradle项目?,netbeans,gradle,libraries,Netbeans,Gradle,Libraries,我有一个NetBeans web项目,用于构建Java、JS和HTML文件。为了使改造工作能够导入JSON API,我需要在项目根目录的build.gradle文件中注入依赖项。我已经编写了一个headless build.gradle文件,我已经测试并正确地处理了依赖关系,现在我如何将项目更改为从build.gradle文件运行,而不是每次都使用ANT并搜索库 您需要build.gradle和settings.gradle文件,并且必须删除build.xml文件(可能还有nbproject/目

我有一个NetBeans web项目,用于构建Java、JS和HTML文件。为了使改造工作能够导入JSON API,我需要在项目根目录的build.gradle文件中注入依赖项。我已经编写了一个headless build.gradle文件,我已经测试并正确地处理了依赖关系,现在我如何将项目更改为从build.gradle文件运行,而不是每次都使用ANT并搜索库

您需要build.gradle和settings.gradle文件,并且必须删除build.xml文件(可能还有nbproject/目录)

我建议使用netbeans gradle插件创建一个gradle项目,复制并修改build.gradle文件

然后,您需要在build.gradle中添加源集,以确定源所在的位置—gradle期望它们位于不同于netbeans ant放置它们的位置

sourceSets {
  main {
    java {
      srcDir 'src'
    }
    resources {
      srcDir 'resources'
    }
  }

  test {
    java {
      srcDir 'test'
    }
    resources {
      srcDir 'testResources'
    }
  }
}
此外,您还需要建立依赖关系。这就是我的样子:

dependencies {
  testCompile group: 'junit', name: 'junit', version: '4.10+'
  compile project(':logger')
  compile project(':postalker')
  compile group: 'org.yaml', name: 'snakeyaml', version: '1.15+'
  compile group: 'commons-io', name: 'commons-io', version: '2.4+'
  compile group: 'gov.nist.math', name: 'jama', version: '1.0.3+'
  compile group: 'org.apache.commons', name: 'commons-imaging', version: '1.0-SNAPSHOT+'
  compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.5+'
  compile files( 'lib/jogl-runtime/jogl.jar')
  compile files( 'lib/gluegen-runtime/gluegen-rt.jar')
  compile files( 'lib/toxiclibscore.jar')
  compile group: 'org.apache.commons', name: 'commons-math3', version: '3.5+'
  compile group: 'commons-codec', name: 'commons-codec', version: '1.6'
  compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3'
  compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.3'
  compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.6'
  compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.3.6'
}
最后,, 我必须关闭旧项目,关闭netbeans,然后重新打开netbeans和项目。做一个重新加载项目,它就启动并运行了

对于想知道我是如何让jogl工作的人,我看了一下jogl插件对ant做了什么,并用gradle复制了它。我添加了一个包含开发人员平台的PlatformPrivate.gradle文件,然后将相应的库复制到build目录

我的整个身材。格雷德尔:

import com.protocase.files.FolderUtils
import java.nio.file.Files
import java.nio.file.Paths
import com.protocase.designer.extractors.BuildInfoExtractor
import com.protocase.designer.fileeditors.NSISInstallerModifier
import com.protocase.designer.fileeditors.SignBatModifier
import com.protocase.designer.ConfigureRun
import com.protocase.finders.FindFiles

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'groovy'

apply from: "PlatformPrivate.gradle"
apply from: "Platforms.gradle"

sourceCompatibility = '1.6'

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.protocase.viewer.JDesigner'
}

if (! hasProperty('localPlatform')) {
  throw new GradleException("Missing PlatformPrivate.gradle");
}


mainClassName = ext.mainClass

repositories {
    mavenCentral()
    maven {
            url "https://repository.apache.org/content/repositories/snapshots/"
    }
}

dependencies {
  testCompile group: 'junit', name: 'junit', version: '4.10+'
  compile project(':logger')
  compile project(':postalker')
  compile group: 'org.yaml', name: 'snakeyaml', version: '1.15+'
  compile group: 'commons-io', name: 'commons-io', version: '2.4+'
  compile group: 'gov.nist.math', name: 'jama', version: '1.0.3+'
  compile group: 'org.apache.commons', name: 'commons-imaging', version: '1.0-SNAPSHOT+'
  compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.5+'
  compile files( 'lib/jogl-runtime/jogl.jar')
  compile files( 'lib/gluegen-runtime/gluegen-rt.jar')
  compile files( 'lib/toxiclibscore.jar')
  compile group: 'org.apache.commons', name: 'commons-math3', version: '3.5+'
  compile group: 'commons-codec', name: 'commons-codec', version: '1.6'
  compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3'
  compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.3'
  compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.6'
  compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.3.6'
}

task copyDistLibs(type: Copy) {
  def outputDir = file("${buildDir}/libs")

  from configurations.runtime

  into outputDir
}

task updateBuildInfoBeta (type: Exec) {
  workingDir "${projectDir}/perl"
  commandLine './updateBuildInfo'
  def extractor = new BuildInfoExtractor(projectFile: new File("${projectDir}"))

  def version = extractor.getBetaVersion()

  def installerModifier = new NSISInstallerModifier(NSISFile: new File("${projectDir}/winRunFiles/JDesigner.nsi"))
  installerModifier.setOutputFileVersion(version)

  def signBatModifier = new SignBatModifier(signBatFile: new File("${projectDir}/sign.bat"))
  signBatModifier.setOutputFileVersion(version)

  println ""
  println "Making Beta Version: " + version
  println "branch: " + extractor.getReleaseVersion() + " date: " + extractor.getBuildDate()
  println ""
}

task makeBeta(dependsOn: updateBuildInfoBeta) {

  def distFolderLinux64 = makeOneDistBeta("linux-amd64")

  def ditsFolderLinux32 = makeOneDistBeta("linux-i586")

  def distFolderWin32 = makeOneDistBeta("windows-i586")
  FolderUtils.copyFolderContents(new File("${projectDir}/jre"), new File(distFolderWin32, "jre"))
  FolderUtils.copyFolderContents(new File("${projectDir}/src/main/resources/library"), new File(distFolderWin32, "library"))
  FolderUtils.copyFolderContents(new File("${projectDir}/winRunFiles"), distFolderWin32)
  Files.copy(Paths.get("${projectDir}/license.txt"), Paths.get(new File(distFolderWin32, "license.txt").path))
  Files.copy(Paths.get("${projectDir}/Protocase Designer.ico"), Paths.get(new File(distFolderWin32, "Protocase Designer.ico").path))


  def distFolderMac = makeOneDistBeta("macosx-universal")

}

private File makeOneDistBeta(def nativePlatform) {
  def plat = new com.protocase.designer.Platform(natives: nativePlatform, libFolder: 'lib', genericBuildDir: new File("${buildDir}"))
  println plat.getNativeDistDir()
  plat.makeAndPopulateBuildDir()
  plat.copyLibraryFiles()
  plat.getNativeDistDir()

}


makeBeta.dependsOn(jar)
makeBeta.dependsOn(copyDistLibs)

task makeWin32Beta(dependsOn: makeBeta, type: Exec) {
  def wineDir = new File("${projectDir}/../../.wine/drive_c/")
  println wineDir.exists()
  def nsisFile = FindFiles.findFirstFileByNameWithoutDirectory(wineDir, "makensis.exe", "users")
  println nsisFile.path

  def MAKENSIS = nsisFile.getPath().replaceFirst("(.*)drive_c/", "c:\\\\")
  println MAKENSIS
  MAKENSIS = MAKENSIS.replaceAll("/","\\\\")

  def extractor = new BuildInfoExtractor(projectFile: new File("${projectDir}"))
  def version = extractor.getBetaVersion()

  workingDir "${buildDir}/windows-i586"


  def fullCommand = 'WINEDEBUG=fixme-win,fixme-sync,fixme-heap /usr/bin/wine "' + MAKENSIS + '" /V1 /DVER_STR=' + version + ' /X"SetCompressor /FINAL lzma" ./JDesigner.nsi'

  println "makensis command:"
  println MAKENSIS
  println fullCommand
  println ""

  commandLine fullCommand
}



private void configureRun (def task) {
  apply from: 'PlatformPrivate.gradle'
  def confRun = new ConfigureRun(localPlatform: localPlatform)

  confRun.configureRun(task, mainClass, sourceSets)
}

run {
  configureRun(it)
}

task(debug, dependsOn: 'classes', type: JavaExec) {
  configureRun(it)
  debug = true
}
com.protocase.designer.Platform:

package com.protocase.designer

import com.protocase.files.FolderUtils
import org.gradle.api.GradleException
import static groovy.io.FileType.FILES
import java.nio.file.StandardCopyOption
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths

public class Platform {
  def natives
  def libFolder
  def genericBuildDir

  String gluegenDir() {
     def dirName = 'gluegen-rt.jar-natives-' + natives
     dirName
  }

  String joglDir() {
    def dirName = "jogl.jar-natives-" + natives
    dirName
  }

  def getExtension() {
    def result = ""
    switch (natives)
    {
      case 'linux-amd64':
      case 'linux-i586':
      case 'solaris-i586':
      case 'solaris-sparc':
      case 'solaris-sparcv9':
        result = 'so'
        break;

      case 'macosx-universal':
      case 'macosx-ppc':
        result = 'jnilib'
        break;

      case 'windows-i586':
      case 'windows-amd64':
        result = 'dll'
        break;

      default:
        throw new GradleException ('programmer missed a case or bad platform: ' + natives)
    }

    result
  }

  def getNativeDistDir() {
    def buildDir = new File(genericBuildDir, natives)
  }

  def makeAndPopulateBuildDir() {
    def sourceDir = new File(genericBuildDir, 'libs')
    def nativeDistDir = getNativeDistDir()

    if (nativeDistDir.exists()) {
      FolderUtils.deleteFolder(nativeDistDir)
    }

    FolderUtils.copyFolderContents(sourceDir, nativeDistDir)
  }

  def getLibraryDirs() {
    def dirList = []

    def dir = new File(new File(libFolder, "gluegen-runtime"), gluegenDir())

    dirList << dir

    dir = new File(new File(libFolder, "jogl-runtime"), joglDir())

    dirList << dir

    dirList
  }

  def getLibraryFiles() {
    def fileList = []

    def dirs = getLibraryDirs()

    dirs.each {
      it.eachFile(FILES) {
    file -> fileList << file
      }
    }

    fileList
  }

  def copyLibraryFiles() {

    def copyOptions = [StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES]
    def destinationFolder = new File(getNativeDistDir(), 'lib')

    getLibraryDirs().each {
      FolderUtils.copyFolderContents(it, destinationFolder)
    }
  }

}
package com.protocase.designer
导入com.protocase.files.FolderUtils
导入org.gradle.api.GradleException
导入静态groovy.io.FileType.FILES文件
导入java.nio.file.StandardCopyOption
导入java.io.xml文件
导入java.nio.file.Files
导入java.nio.file.path
公共课堂平台{
def本地人
def libFolder
def genericbuildir
字符串gluegenDir(){
def dirName='gluegen-rt.jar-natives-'+natives
肮脏的
}
字符串joglDir(){
def dirName=“jogl.jar本地人-”+本地人
肮脏的
}
def getExtension(){
def result=“”
交换机(本地)
{
案例“linux-amd64”:
案例“linux-i586”:
案例“solaris-i586”:
案例“solaris sparc”:
案例“solaris-sparcv9”:
结果='so'
打破
“macosx通用”案例:
案例“macosx ppc”:
结果='jnilib'
打破
案例“windows-i586”:
案例“windows-amd64”:
结果='dll'
打破
违约:
抛出新的GradleException('程序员错过了一个案例或糟糕的平台:'+本地人)
}
结果
}
def getNativeDistDir(){
def buildDir=新文件(genericBuildDir,本机)
}
def makeAndPopulateBuildDir(){
def sourceDir=新文件(genericBuildDir,'libs')
def nativeDistDir=getNativeDistDir()
如果(nativeDistDir.exists()){
deleteFolder(nativeDistDir)
}
copyFolderContents(sourceDir、nativeDistDir)
}
def getLibraryDirs(){
def dirList=[]
def dir=新文件(新文件(libFolder,“gluegen运行时”),gluegenDir()

dirList嗨,我在让jogl和gradle一起工作时遇到了问题,我在你的
依赖项中看到你使用它…你介意显示完整的
构建.gradle
?我添加了我的full build.gradle和我制作的其他几个gradle文件。如果我遗漏了什么,请告诉我。请注意,他使用jogl 1时,你会看到jogl.jar above尽管在jogl 2中它被称为jogl-all.jar。jogl 1已经过时,大约5年后就不再维护了,现在是时候切换到jogl 2了。但是如果您已经使用jogl 1很长时间了,它仍然有效!jogl 2的原理是一样的。
package com.protocase.designer

import com.protocase.files.FolderUtils
import org.gradle.api.GradleException
import static groovy.io.FileType.FILES
import java.nio.file.StandardCopyOption
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths

public class Platform {
  def natives
  def libFolder
  def genericBuildDir

  String gluegenDir() {
     def dirName = 'gluegen-rt.jar-natives-' + natives
     dirName
  }

  String joglDir() {
    def dirName = "jogl.jar-natives-" + natives
    dirName
  }

  def getExtension() {
    def result = ""
    switch (natives)
    {
      case 'linux-amd64':
      case 'linux-i586':
      case 'solaris-i586':
      case 'solaris-sparc':
      case 'solaris-sparcv9':
        result = 'so'
        break;

      case 'macosx-universal':
      case 'macosx-ppc':
        result = 'jnilib'
        break;

      case 'windows-i586':
      case 'windows-amd64':
        result = 'dll'
        break;

      default:
        throw new GradleException ('programmer missed a case or bad platform: ' + natives)
    }

    result
  }

  def getNativeDistDir() {
    def buildDir = new File(genericBuildDir, natives)
  }

  def makeAndPopulateBuildDir() {
    def sourceDir = new File(genericBuildDir, 'libs')
    def nativeDistDir = getNativeDistDir()

    if (nativeDistDir.exists()) {
      FolderUtils.deleteFolder(nativeDistDir)
    }

    FolderUtils.copyFolderContents(sourceDir, nativeDistDir)
  }

  def getLibraryDirs() {
    def dirList = []

    def dir = new File(new File(libFolder, "gluegen-runtime"), gluegenDir())

    dirList << dir

    dir = new File(new File(libFolder, "jogl-runtime"), joglDir())

    dirList << dir

    dirList
  }

  def getLibraryFiles() {
    def fileList = []

    def dirs = getLibraryDirs()

    dirs.each {
      it.eachFile(FILES) {
    file -> fileList << file
      }
    }

    fileList
  }

  def copyLibraryFiles() {

    def copyOptions = [StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES]
    def destinationFolder = new File(getNativeDistDir(), 'lib')

    getLibraryDirs().each {
      FolderUtils.copyFolderContents(it, destinationFolder)
    }
  }

}