Get started with maven

后端 未结 3 1712
日久生厌
日久生厌 2021-02-06 19:11

I have experience in Ant, how to get started with maven. Any advantage with maven over ant?

3条回答
  •  生来不讨喜
    2021-02-06 19:35

    There's a quite large difference, where ant forces you to create your own targets you will get a default set of targets for maven, e.g., clean, install, package etc without scripting them.

    Maven promotes that you use a common directory structure for java classes, resources etc. If you do, maven is just on xml file where you specify some project metadata such as name, package and most importantly depenencies. It provides similar dependency lookup to what ivy does for ant.

    Based on the standard maven promotes, it becomes very easy for developers to approach and build your projects. With an IDE such as Netbeans it's enough to select open project, and then hit the install button to compile and install the project in your local repository.

    I recommend working with maven the maven way. Doing things differently will often cause more pain than it's worth. Maven offers a plugin structure where you can perform various tasks, such as invoke the ant-library should you need to. If you're actively working with multiple projects (and want project switching to be as easy as possible) maven is a huge leap forward, especially if combined with repository server such as Nexus or Archiva.

    To get started

    Either you can generate your project structure using the archetype goal of maven, or you could do it the way I do by copy-pasing an empty template project every time. Then you need the maven binary and the project definition file pom.xml which I typically also copy paste between projects.

    A sample is included below. With this sample you'll get the external library log4j, and you automatically get all nececcities to build and package your own project (in this case to a jar file).

    
        4.0.0
        com.company
        projectname
        jar
        0.1.0-SNAPSHOT
        ${project.artifactId}
        http://maven.apache.org
    
        
            
                log4j
                log4j
                1.2.16
            
        
        
    
    
        
            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    
                        1.6
                        1.6
                    
                
            
        
    
    
    

提交回复
热议问题