Maven2入门--超级简单
maven2 能做什么? maven 可以帮我们更好的管理软件,包括编译,文档生成,管理项目依赖等。 用 maven 来替代 ant
maven2 能做什么?
maven 可以帮我们更好的管理软件,包括编译,文档生成,管理项目依赖等。
用 maven 来替代 ant
j2ee 下 ant 早已是事实上的标准。但是,每次开发一个新的项目,总要从头写 ant 的 buildfile (当然,大多数情况直接 copy 旧的 buildfile )。用 maven 则可省去很多步骤,因为它知道该怎么编译项目。
看看 maven 怎么开始一个新 java 项目:
1. 创建一个项目模板,其中 groupId 是程序的 package , artifactId 是项目的名字。这里假定你的项目的 package 是 tutorial, 名称是 tutorial :
> mvn archetype:create -DgroupId=tutorial -DartifactId=tutorial
| [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Default Project [INFO] task-segment: [archetype:create] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'. [INFO] Setting property: velocimacro.messages.on => 'false'. [INFO] Setting property: resource.loader => 'classpath'. [INFO] Setting property: resource.manager.logwhenfound => 'false'. [INFO] ************************************************************** [INFO] Starting Jakarta Velocity v1.4 [INFO] RuntimeInstance initializing. [INFO] Default Properties File: org/apache/velocity/runtime/defaults/velocity.properties [INFO] Default ResourceManager initializing. (class org.apache.velocity.runtime.resource.ResourceManagerImpl) [INFO] Resource Loader Instantiated: org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader [INFO] ClasspathResourceLoader : initialization starting. [INFO] ClasspathResourceLoader : initialization complete. [INFO] ResourceCache : initialized. (class org.apache.velocity.runtime.resource.ResourceCacheImpl) [INFO] Default ResourceManager initialization complete. [INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Literal [INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Macro [INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Parse [INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Include [INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Foreach [INFO] Created: 20 parsers. [INFO] Velocimacro : initialization starting. [INFO] Velocimacro : adding VMs from VM library template : VM_global_library.vm [ERROR] ResourceManager : unable to find resource 'VM_global_library.vm' in any resource loader. [INFO] Velocimacro : error using VM library template VM_global_library.vm : org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'VM_global_library.vm' [INFO] Velocimacro : VM library template macro registration complete. [INFO] Velocimacro : allowInline = true : VMs can be defined inline in templates [INFO] Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT replace previous VM definitions [INFO] Velocimacro : allowInlineLocal = false : VMs defined inline will be global in scope if allowed. [INFO] Velocimacro : initialization complete. [INFO] Velocity successfully started. [INFO] [archetype:create] [INFO] Defaulting package to group ID: tutorial [INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking for updates from central [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating Archetype: maven-archetype-quickstart:RELEASE [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: tutorial [INFO] Parameter: packageName, Value: tutorial [INFO] Parameter: package, Value: tutorial [INFO] Parameter: artifactId, Value: tutorial [INFO] Parameter: basedir, Value: G:/tmp [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] ********************* End of debug info from resources from generated POM *********************** |
2. 现在,看看生成了什么:
> tree /f /a
| G:. /---tutorial | pom.xml | /---src +---main | /---java | /---tutorial | App.java | /---test /---java /---tutorial AppTest.java |
3. 好了,编译模板程序看看 ( 直接打包成 jar 文件 ) :
> cd tutorial
> mvn install
| [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building tutorial [INFO] task-segment: [install] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:compile] [INFO] Compiling 1 source file to G:/tmp/tutorial/target/classes [INFO] [resources:testResources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:testCompile] [INFO] Compiling 1 source file to G:/tmp/tutorial/target/test-classes [INFO] [surefire:test] [INFO] Surefire report directory: G:/tmp/tutorial/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running tutorial.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.078 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [jar:jar] [INFO] Building jar: G:/tmp/tutorial/target/tutorial-1.0-SNAPSHOT.jar [INFO] [install:install] [INFO] Installing G:/tmp/tutorial/target/tutorial-1.0-SNAPSHOT.jar to C:/Documents and Settings/guest/.m2/repository/tutorial/tutorial/1.0-SNAPSHOT/tutorial-1.0-SNAPSHOT.j [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ |
4. 运行看看:
> java -cp target/tutorial-1.0-SNAPSHOT.jar tutorial.App
不需要写复杂的 ant buildfile ,简单的命令,省了许多麻烦。
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!