添加 Gradle 我们可以到我们添加 Maven 依赖的网站
Maven Repository: Search/Browse/Explore
http://mvnrepository.com/
上查找,例如我们搜索 spring-core 的依赖
点击 Gradle 选项卡,查看 Gradle 依赖的写法。
IDEA 工具默认帮助我们生成的 build.gradle
group 'com.winner' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'war' apply plugin: 'idea' //指定编译.java文件的jdk版本 sourceCompatibility = 1.8 repositories { //使gradle项目能够使用本地仓库 mavenLocal() mavenCentral() } dependencies { //testCompile相当于pom.xml中scope值是test testCompile group: 'junit', name: 'junit', version: '4.11' compile group: 'org.springframework', name: 'spring-core', version: '4.3.3.RELEASE' }
关于mavenLocal()的说明
/** * Adds a repository which looks in the local Maven cache for dependencies. The name of the repository is * {@value org.gradle.api.artifacts.ArtifactRepositoryContainer#DEFAULT_MAVEN_LOCAL_REPO_NAME}. * * <p>Examples: * <pre autoTested=""> * repositories { * mavenLocal() * } * </pre> * </p> * <p> * The location for the repository is determined as follows (in order of precedence): * <ol> * <li>The value of system property 'maven.repo.local' if set;</li> * <li>The value of element <localRepository> of <code>~/.m2/settings.xml</code> if this file exists and element is set;</li> * <li>The value of element <localRepository> of <code>$M2_HOME/conf/settings.xml</code> (where <code>$M2_HOME</code>
is the value of the environment variable with that name) if this file exists and element is set;</li> * <li>The path <code>~/.m2/repository</code>.</li> * </ol> * </p> * * @return the added resolver */ MavenArtifactRepository mavenLocal();