<mirror> <id>CN</id> <name>OSChina Central</name> <url>http://maven.oschina.net/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> ======================================================== OSChina Maven 库ä‹É用帮åŠ? ==================å…¶ä»–maven仓库镜åƒ========================== <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> <mirror> <id>net-cn</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://maven.net.cn/content/groups/public/</url> </mirror> <mirror> <id>ui</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://uk.maven.org/maven2/</url> </mirror> <mirror> <id>ibiblio</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url> </mirror> <mirror> <id>jboss-public-repository-group</id> <mirrorOf>central</mirrorOf> <name>JBoss Public Repository Group</name> <url>http://repository.jboss.org/nexus/content/groups/public</url> </mirror>
<mirror> <id>JBossJBPM</id>
    <mirrorOf>central</mirrorOf>
    <name>JBossJBPM Repository</name>
    <url>https://repository.jboss.org/nexus/content/repositories/releases/</url>
</mirror>
Maven会沿ç€çˆ¶å层次å‘上赎ͼŒç›´åˆ°æ‰‘Öˆ°ä¸€ä¸ªæ‹¥æœ‰dependencyManagementå…ƒç´ çš„é¡¹ç›®ï¼Œç„¶åŽå®ƒå°±ä¼šä‹É用在˜q™ä¸ªdependencyManagementå…ƒç´ ä¸æŒ‡å®šçš„版本å—÷€?/p>
ç„¶åŽåQŒåœ¨å项目ä¸åQŒä½ å¯ä»¥ä½¿ç”¨å¦‚下的ä¾èµ–XMLæ·ÕdŠ ä¸€ä¸ªå¯¹MySQL Java Connectorçš„ä¾èµ–:
如果mysql-connector-java在顶层父™å¹ç›®ä¸è¢«ä½œäؓ一个ä¾èµ–列出,˜q™ä¸ªå±‚次ä¸çš„æ‰€æœ‰é¡¹ç›®éƒ½ž®†å¼•用该ä¾èµ–。ä‹É用dependencyManagementèƒ½è®©ä½ ç»Ÿä¸€òq‰™›†ä¸åŒ–ä¾èµ–版本的管ç†ï¼Œè€Œä¸ç”¨æ·»åŠ é‚£äº›ä¼šè¢«æ‰€æœ‰å™å¹ç›®¾l§æ‰¿çš„ä¾èµ–ã€?/p>
解决˜q™ä¸ªé—®é¢˜çš„æ–¹æ³•就是ä‹Éç”?lt;scope>æ ‡ç¾åQŒå¦‚ä¸?/p>
˜q™é‡Œçš„æ„æ€æ˜¯è¯ß_¼Œ¾~–译的时候用到servlet-apiå’Œjsp-apiåQŒä½†åœ¨æ‰“包的时候ä¸ç”¨è¿™ä¸¤ä¸ªä¾èµ–ã€?/p>
在mavençš„å®˜æ–¹ä¸æ˜¯è¿™æ äh˜q°çš„
Dependency Scope
Dependency scope is used to limit the transitivity of a depedency, and also to affect the classpath used for various build tasks.
There are 6 scopes available:
compile
This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
runtime
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
test
This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.
system
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
import (only available in Maven 2.0.9 or later)
This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
å¦‚æžœä½ è¿™æ ·åšäº†ï¼Œä½†ä‹É用eclipse+tomcatåšæµ‹è¯•的时候å‘现servlet-api˜q˜æ˜¯è¢«æ‰“包到lib下é¢äº†ï¼Œä½ è¦æŠŠmavenæ’äšgä¸çš„WTP也安装一下,问题应该ž®Þp§£å†³äº†ã€?br />
private static final char PATH_SEPARATOR = '/';
private static final char GROUP_SEPARATOR = '.';
private static final char ARTIFACT_SEPARATOR = '-';
public String pathOf( Artifact artifact )
{
ArtifactHandler artifactHandler = artifact.getArtifactHandler();
StringBuilder path = new StringBuilder( 128 );
path.append( formatAsDirectory( artifact.getGroupId() ) ).append( PATH_SEPARATOR );
path.append( artifact.getArtifactId() ).append( PATH_SEPARATOR );
path.append( artifact.getBaseVersion() ).append( PATH_SEPARATOR );
path.append( artifact.getArtifactId() ).append( ARTIFACT_SEPARATOR ).append( artifact.getVersion() );
if ( artifact.hasClassifier() )
{
path.append( ARTIFACT_SEPARATOR ).append( artifact.getClassifier() );
}
if ( artifactHandler.getExtension() != null && artifactHandler.getExtension().length() > 0 )
{
path.append( GROUP_SEPARATOR ).append( artifactHandler.getExtension() );
}
return path.toString();
}
private String formatAsDirectory( String directory )
{
return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
}
??
该pathOf()æ–ÒŽ³•的目的是æ ÒŽ®æž„äšgä¿¡æ¯ç”Ÿæˆå…¶åœ¨ä»“库ä¸çš„路径。在阅读本段代ç 之å‰åQŒè¯»è€…å¯ä»¥å…ˆå›žé¡¾ä¸€ä¸‹ä¸Šä¸€ç« Mavenåæ ‡çš„相兛_†…å®V€‚这里,我们æ ÒŽ®ä¸€ä¸ªå®žé™…çš„ä¾‹åæ¥åˆ†æžèµ\径的生æˆåQŒè€ƒè™‘˜q™æ ·ä¸€ä¸ªæž„ä»Óž¼šgroupId=org.testngã€artifactId=testngã€version=5.8ã€classifier=jdk15ã€packaging=jaråQŒå…¶å¯¹åº”çš„èµ\径按如下æ¥éª¤ç”ŸæˆåQ?/p>
首先åŸÞZºŽæž„äšgçš„groupId准备路径åQŒformatAsDirectory()ž®†groupIdä¸çš„å¥ç‚¹åˆ†éš”½W¦è{æ¢æˆè·¯å¾„分隔½W¦ï¼Œè¯¥ä¾‹ä¸ï¼ŒgroupId org.testngž®×ƒ¼šè¢«è{æ¢æˆorg/testngåQŒä¹‹åŽå†åŠ ä¸€ä¸ªèµ\径分隔符斜æ åQŒé‚£ä¹ˆorg.testngž®±æˆä¸ÞZº†org/testng/ã€?
åŸÞZºŽæž„äšgçš„artifactId准备路径åQŒä¹Ÿž®±æ˜¯åœ¨å‰é¢çš„åŸºç¡€ä¸ŠåŠ ä¸ŠartifactId以åŠä¸€ä¸ªèµ\径分隔符åQŒè¯¥ä¾‹ä¸çš„artifactId为testngåQŒé‚£ä¹ˆåœ¨˜q™ä¸€æ¥è¿‡åŽèµ\径就æˆäؓ了org/testng/testng/ã€?
接ç€ä½¿ç”¨ç‰ˆæœ¬ä¿¡æ¯åQŒåœ¨å‰é¢çš„基¼‹€ä¸ŠåŠ ä¸Šversionå’Œèµ\径分隔符åQŒè¯¥ä¾‹ä¸ç‰ˆæœ¬æ˜?.8åQŒé‚£ä¹ˆèµ\径就æˆäؓ了org/testng/tesgng/5.8/ã€?
˜q™ä¸€æ¥å†ä¾æ¬¡åŠ ä¸ŠartifactIdåQŒæž„件分隔符˜qžå—åøP¼Œä»¥åŠversionåQŒäºŽæ˜¯æž„建的路径ž®±å˜æˆäº†org/testng/testng/5.8/testng-5.8。读者å¯èƒ½ä¼šæ³¨æ„到这里ä‹É用了artifactId.getVersion()åQŒè€Œä¸Šä¸€æ¥ç”¨çš„æ˜¯artifactId.getBaseVersion()åQŒversionå’ŒbaseVersionçš„åŒºåˆ«åœ¨æœ¬ç« è®¨è®ºSNAPSHOT的时候会具体é˜è¿°ã€?
紧接ç€å¦‚果构äšg有classifieråQŒå°±åŠ ä¸Šæž„äšg分隔½W¦å’ŒclassifieråQŒè¯¥ä¾‹ä¸æž„äšgçš„classifier是jdk15åQŒé‚£ä¹ˆèµ\å¾„å°±å˜æˆorg/testng/testng/5.8/testng-5.8-jdk5ã€?
最åŽç¬¬‹‚€æŸ¥æž„ä»¶çš„extensionåQŒè‹¥extensionå˜åœ¨åQŒåˆ™åŠ ä¸Šå¥ç‚¹åˆ†éš”½W¦å’ŒextensionåQŒä»Žä»£ç ä¸å¯ä»¥çœ‹åˆŽÍ¼Œextension是从artifactHandler而éžartifact获å–åQŒartifactHandler是由™å¹ç›®çš„packaging军_®šçš„ï¼Œå› æ¤å¯ä»¥è¯ß_¼Œpackaging军_®šäº†æž„件的扩展å,该例的packaging是的jaråQŒå› æ¤æœ€¾lˆçš„路径为org/testng/testng/5.8/testng-5.8-jdk5.jarã€?
åˆ°è¿™é‡Œç¬”è€…ï¼ˆåŒ…æ‹¬è¯»è€…ä½ åQ‰éƒ½åº”该感谢Mavenå¼€æºç¤¾åŒºï¼Œæ£æ˜¯ç”׃ºŽMaven的所有æºä»£ç 都是开攄¡š„åQŒæˆ‘们æ‰èƒ½ä»”¾l†å¾—深入到其内部工作的所有细节ã€?
ç”׃ºŽMavenä»“åº“æ˜¯åŸºäºŽç®€å•æ–‡ä»¶ç³»¾lŸå˜å‚¨çš„åQŒçŽ°åœ¨æˆ‘ä»¬åˆç†è§£äº†å…¶å˜å‚¨æ–¹å¼åQŒå› æ¤å½“é‡åˆ°ä¸€äº›ä¸Žä»“库相关的问题时åQŒå¯ä»¥å¾ˆæ–¹ä¾¿çš„æŸ¥æ‰„¡›¸å…Ïx–‡ä»Óž¼Œæ–¹ä¾¿å®šä½é—®é¢˜ã€‚例如当Mavenæ— æ³•èŽ·å¾—™å¹ç›®å£°æ˜Žçš„ä¾èµ–æ—¶åQŒå¯ä»¥ç®€å•该ä¾èµ–å¯¹åº”çš„æ–‡ä»¶åœ¨ä»“åº“ä¸æ˜¯å¦å˜åœ¨ï¼Œå¦‚æžœä¸å˜åœ¨ï¼Œæ˜¯å¦æœ‰å…¶å®ƒç‰ˆæœ¬å¯ç”¨ï¼Œ½{‰ç‰ã€?