3.7.2 依赖存储库 - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
3.7.2 依赖存储库
Remote Repositories
Initially your BuildConfig.groovy does not use any remote public Maven repositories. There is a defaultgrailsHome() repository that will locate the JAR files Grails needs from your Grails installation. To use a public repository, specify it in the repositories block:repositories {
mavenCentral()
}ebr() method:repositories {
ebr()
}repositories {
mavenRepo "http://repository.codehaus.org"
}repositories {
mavenRepo name: "Codehaus", root: "http://repository.codehaus.org"
}远程存储仓库
刚创建的BuildConfig.groovy文件并没有使用任何远程的公共Maven存储库,在那里只有一个缺省的grailsHome()用以从Grails安装目录定位所需要的JAR文件。要使用远程的公共存储库,请使用 repositories代码块,比如:repositories {
mavenCentral()
}ebr()方法:repositories {
ebr()
}repositories {
mavenRepo "http://repository.codehaus.org"
}repositories {
mavenRepo name: "Codehaus", root: "http://repository.codehaus.org"
}Controlling Repositories Inherited from Plugins
A plugin you have installed may define a reference to a remote repository just as an application can. By default your application will inherit this repository definition when you install the plugin.If you do not wish to inherit repository definitions from plugins then you can disable repository inheritance:repositories {
inherit false
}控制插件存储库地继承
通常,你安装的插件会定义一个远程的存储库,缺省情况下,而你的应用将继承你安装插件中的存储库的定义。如果你不希望继承来自插件的存储库定义,你可以禁止这种存储库的继承:repositories {
inherit false
}Offline Mode
There are times when it is not desirable to connect to any remote repositories (whilst working on the train for example!). In this case you can use theoffline flag to execute Grails commands and Grails will not connect to any remote repositories:grails --offline run-app
Note that this command will fail if you do not have the necessary dependencies in your local Ivy cacheYou can also globally configure offline mode by setting
grails.offline.mode to true in ~/.grails/settings.groovy or in your project's BuildConfig.groovy file:grails.offline.mode=true离线模式
偶尔的时候,你将不能访问任何远程的存储库(比如在火车上工作时)。此种情况下,你可以使用offline标志来执行Grails命令,这样Grails将不会连接任何远程存储库,比如:grails --offline run-app
注意:如果你本地Ivy缓存中没有所需的依赖,上述命令将会出错。你还可以通过设置
~/.grails/settings.groovy或者工程中BuildConfig.groovy文件中的grails.offline.mode为true的方式,将其配置为全局离线模式:grails.offline.mode=trueLocal Resolvers
If you do not wish to use a public Maven repository you can specify a flat file repository:repositories {
flatDir name:'myRepo', dirs:'/path/to/repo'
}~/.m2/repository) as a repository:repositories {
mavenLocal()
}本地解析器
如果你不希望使用远程的Maven存储库,你可以指定一个平面文件(flat file)存储器:repositories {
flatDir name:'myRepo', dirs:'/path/to/repo'
}~/.m2/repository)作为存储器,可以用下面的处理:repositories {
mavenLocal()
}Custom Resolvers
If all else fails since Grails builds on Apache Ivy you can specify an Ivy resolver:/* * Configure our resolver. */ def libResolver = new org.apache.ivy.plugins.resolver.URLResolver() ['libraries', 'builds'].each { libResolver.addArtifactPattern( "http://my.repository/${it}/" + "[organisation]/[module]/[revision]/[type]s/[artifact].[ext]") libResolver.addIvyPattern( "http://my.repository/${it}/" + "[organisation]/[module]/[revision]/[type]s/[artifact].[ext]") }libResolver.name = "my-repository" libResolver.settings = ivySettingsresolver libResolver
import org.apache.ivy.plugins.resolver.SshResolver … repositories { ... def sshResolver = new SshResolver( name: "myRepo", user: "username", host: "dev.x.com", keyFile: new File("/home/username/.ssh/id_rsa"), m2compatible: true) sshResolver.addArtifactPattern( "/home/grails/repo/[organisation]/[artifact]/" + "[revision]/[artifact]-[revision].[ext]") sshResolver.latestStrategy = new org.apache.ivy.plugins.latest.LatestTimeStrategy() sshResolver.changingPattern = ".*SNAPSHOT" sshResolver.setCheckmodified(true) resolver sshResolver }
grails -classpath /path/to/jsch compile|run-app|etc.
CLASSPATH environment variable but be aware this it affects many Java applications. An alternative on Unix is to create an alias for grails -classpath ... so that you don't have to type the extra arguments each time.
自定义解析器
如果上述的都失败了,你还可以自定义一个Ivy解析器,因为Grails是基于Apache Ivy构建的:/* * Configure our resolver. */ def libResolver = new org.apache.ivy.plugins.resolver.URLResolver() ['libraries', 'builds'].each { libResolver.addArtifactPattern( "http://my.repository/${it}/" + "[organisation]/[module]/[revision]/[type]s/[artifact].[ext]") libResolver.addIvyPattern( "http://my.repository/${it}/" + "[organisation]/[module]/[revision]/[type]s/[artifact].[ext]") }libResolver.name = "my-repository" libResolver.settings = ivySettingsresolver libResolver
import org.apache.ivy.plugins.resolver.SshResolver … repositories { ... def sshResolver = new SshResolver( name: "myRepo", user: "username", host: "dev.x.com", keyFile: new File("/home/username/.ssh/id_rsa"), m2compatible: true) sshResolver.addArtifactPattern( "/home/grails/repo/[organisation]/[artifact]/" + "[revision]/[artifact]-[revision].[ext]") sshResolver.latestStrategy = new org.apache.ivy.plugins.latest.LatestTimeStrategy() sshResolver.changingPattern = ".*SNAPSHOT" sshResolver.setCheckmodified(true) resolver sshResolver }
grails -classpath /path/to/jsch compile|run-app|etc.
CLASSPATH环境变量中,不要要注意,这可能会影响很多的Java应用。在Unix下,你可以为grails -classpath ...创建一个别名,这样就可以不需要每次都敲额外的参数了。Authentication
If your repository requires authentication you can configure this using acredentials block:credentials {
realm = ".."
host = "localhost"
username = "myuser"
password = "mypass"
}USER_HOME/.grails/settings.groovy file using the grails.project.ivy.authentication setting:grails.project.ivy.authentication = {
credentials {
realm = ".."
host = "localhost"
username = "myuser"
password = "mypass"
}
}验证
如果你的存储仓库需要验证,那么你可以通过credentials代码块来进行配置,比如:credentials {
realm = ".."
host = "localhost"
username = "myuser"
password = "mypass"
}USER_HOME/.grails/settings.groovy文件中的grails.project.ivy.authentication来实现:grails.project.ivy.authentication = {
credentials {
realm = ".."
host = "localhost"
username = "myuser"
password = "mypass"
}
}
