简体   繁体   English

Groovy - 如何建立一个罐子

[英]Groovy - How to Build a Jar

I've written a Groovy script which has a dependency on a SQL Server driver (sqljdbc4.jar). 我编写了一个Groovy脚本,它依赖于SQL Server驱动程序(sqljdbc4.jar)。 I can use the GroovyWrapper (link below) to compile it into a JAR, however how can I get dependencies into the Jar? 我可以使用GroovyWrapper(下面的链接)将其编译成JAR,但是如何才能将依赖项添加到Jar中? I'm looking for a "best practice" sort of thing. 我正在寻找一种“最佳实践”的东西。

https://github.com/sdanzan/groovy-wrapper https://github.com/sdanzan/groovy-wrapper

Both of the replies below have been helpful, but how can I do this for signed Jar files? 下面的两个回复都有帮助,但是如何为签名的Jar文件执行此操作? For instance: 例如:

Exception in thread "main" java.lang.SecurityException: Invalid signature file d igest for Manifest main attributes 线程“main”中的异常java.lang.SecurityException:Manifest主要属性的签名文件d igest无效

In the groovy wrapper script, you'll see this line near the bottom: 在groovy包装器脚本中,您将在底部附近看到此行:

// add more jars here

That's where you can add your dependencies. 这是您可以添加依赖项的地方。 If the jar file is in the same directory you're building from, add a line like this: 如果jar文件位于您正在构建的同一目录中,请添加如下所示的行:

zipgroupfileset( dir: '.', includes: 'sqljdbc4.jar' )

Then rerun the script and your jar will include the classes from sqljdbc4.jar . 然后重新运行脚本,你的jar将包含sqljdbc4.jar的类。

Edit: 编辑:

If the jar file you depend on is signed and you need to maintain the signature, you'll have to keep the external jar. 如果您所依赖的jar文件已签名且您需要维护签名,则必须保留外部jar。 You can't include jar files inside of other jar files without using a custom classloader. 如果不使用自定义类加载器,则不能在其他jar文件中包含jar文件。 You can, however, specify the dependency in the manifest to avoid having to set the classpath, ie your jar still executable with java -jar myjar.jar . 但是,您可以在清单中指定依赖项,以避免必须设置类路径,即您的jar仍然可以使用java -jar myjar.jar Update the manifest section in the wrapping script to: 将包装脚本中的清单部分更新为:

manifest {
    attribute( name: 'Main-Class', value: mainClass )
    attribute( name: 'Class-Path', value: 'sqljdbc4.jar' )
}

From your link, if you look at the source of the GroovyWrapper script, there's this line: 从你的链接,如果你看一下GroovyWrapper脚本的来源,就有这一行:

zipgroupfileset( dir: GROOVY_HOME, includes: 'embeddable/groovy-all-*.jar' )
zipgroupfileset( dir: GROOVY_HOME, includes: 'lib/commons*.jar' )
// add more jars here

I'd explicitly add it there. 我明确地将它添加到那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM