How to use maven to manage dependencies for projects that already use ant

Maven is a good thing for managing large projects. Of course, it can also be used only for dependency management (similar to IVY), but we have some projects that have been built using ant, so how can we have both? With small changes to ant's build.xml, it can support the use of maven for dependency management, or ant for build. --- Of course, simple builds do not require ant, and maven is fully capable. Multi-PHP server implementation  Some features of running Maven concurrently in multiple sessions


  •   Can facilitate dependency management
  •   Provides a standard directory structure to ensure that everyone is unified. (You can also override the default settings)
  •   Use profile to solve test and production environment configuration problems
  •   Provides standard build tasks, no need to write by yourself
  •   Multiple projects can be managed centrally and managed in a tree structure at a glance. The development environment can be quickly established. Basically, the project can be compiled in a few steps. On the contrary, in the manual mode, each project has to configure dependencies and it will be exhausting.
  •   Based on the agreed method, there are some troubles in handling special cases, and in some cases ant is more flexible and convenient.




Our goal 

1. For people who like maven, they can quickly use maven to create new projects without having to find dependencies one by one. As ordinary developers, they can not care about complex build.xml, or use maven to build
2. For projects that originally used ant scripts, when compatibility with maven is required, only maven dependency management is added. At the same time, ant can provide automatic collection of dependency packages without manually finding it.
3. Promote the use of maven, rapid creation and development Environment. 
4. Subversion is not saving piles of huge jar packages


. The content of this article is mainly for the second item above. Of course, I hope to implement maven in the end to facilitate management.

First, we must install ant, maven2, and configure environment variables. , Modify the cache path of maven. 

Then install the http://maven.apache.org/ant-tasks/index.html package, which can be placed directly in the lib directory of ant, of course, there are other ways, such as in build.xml Specify the path.


Prepare pom.xml  . For the situation described in this article, you only need to write basic information and dependent package configuration. For

example:

 

<?  xml version="1.0" encoding="UTF-8"  ?> 
<  project   xmlns  ="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi  ="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation  ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"  > 
      <  modelVersion  > 4.0.0  </  modelVersion  > 

      <  groupId  > com.test  </  groupId  > 
      <  artifactId  > maventest  </  artifactId  > 
      <  name  > Maven Test  </  name  > 
      <  version  > 1.0  </  version  > 

      <  build  > 
          <  plugins  > 
              <  plugin  > 
                  <  groupId  > org.apache.maven.plugins  </  groupId  > 
                  <  artifactId  > maven-compiler-plugin  </  artifactId  > 
                  <  configuration  > 
                      <  source  > 1.6  </  source  > 
                      <  target  > 1.6  </  target  > 
                  </  configuration  > 
              </  plugin  > 
          </  plugins  > 
      </  build  > 

      <  dependencies  > 
          <  dependency  > 
              <  groupId  > org.jgroups  </  groupId  > 
              <  artifactId  > jgroups  </  artifactId  > 
              <  version  > 2.10.0.GA  </  version  > 
          </  dependency  > 
          <  dependency  > 
              <  groupId  >  commons-lang  </  groupId  > 
              <  artifactId  >  commons-lang  </  artifactId  > 
              <  version  >  2.5  </  version  > 
          </  dependency  > 
          <  dependency  > 
            <  groupId  >  junit  </  groupId  > 
            <  artifactId  >  junit  </  artifactId  > 
            <  version  >  3.8.1  </  version  > 
            <  scope  > test  </  scope  > 
          </  dependency  > 
      </  dependencies  > 

</  project  > 



If you have a local warehouse, you can also configure the repository. (It is recommended to use the local server first). The content of this file is very small, mainly the information of the dependent package, which is also the information we are most concerned about.


Let's modify our build.xml to ant Provide dependent services:


<  project   name  ="jgroups"   basedir  ="."   xmlns:artifact   ="urn:maven-artifact-ant"     > 

      <!--   存放依赖包的目录   --> 
      <  property   name  ="build.lib.dir"   location  ="./lib"     /> 

      <!--   find maven-ant-tasks, you can use classpathref to specify the jar, or just copy the jar to ant/lib    --> 
      <  typedef    resource   ="org/apache/maven/artifact/ant/antlib.xml"    uri  ="urn:maven-artifact-ant"      /> 

      <!--   maven的文件   --> 
      <  artifact:pom    id   ="maven.project"    file   ="pom.xml"       /> 

      <  artifact:dependencies   filesetId  ="deps.fileset.compile"   useScope  ="compile"  > 
          <!--  <pom file="pom.xml"/>  --> 
          <  pom   refid  ="maven.project"      /> 
      </  artifact:dependencies  > 

      <  artifact:dependencies   filesetId  ="deps.fileset.test"   scopes  ="test"  > 
          <  pom   refid  ="maven.project"      /> 
      </  artifact:dependencies  > 

      <  artifact:dependencies   filesetId  ="deps.fileset.provide"   scopes  ="provide"  > 
          <  pom   refid  ="maven.project"      /> 
      </  artifact:dependencies  > 

      <  target   name  ="lib.init"  > 
          <  copy   todir  ="${build.lib.dir}/compile"  > 
              <  fileset   refid  ="deps.fileset.compile"  /> 
              <  mapper   type  ="flatten"  /> 
          </  copy  > 
          <  copy   todir  ="${build.lib.dir}/test"  > 
              <  fileset   refid  ="deps.fileset.test"  /> 
              <  mapper   type  ="flatten"  /> 
          </  copy  > 
          <  copy   todir  ="${build.lib.dir}/provide"  > 
              <  fileset   refid  ="deps.fileset.test"  /> 
              <  mapper   type  ="flatten"  /> 
          </  copy  > 
      </  target  > 


      <!--    ..其他 ..       --> 


</  project  > 




A target is mainly added, which is to copy the dependency package defined in maven to the specified directory, without having to prepare it yourself or download it from subversion.

(Other targets of ant can also use several variables defined as the classpath)

Generally, the dependencies are divided into three Please pay attention to the distinction, otherwise, if you publish the jar package such as servlet to the server, you will usually get an error:

  •   compile: Compile, used at runtime and compile time, packaged and released to include
  •   provide: classes provided by the container, mainly used for compilation, not included when packaged and released, such as servlet-api
  •   test: Only used to compile and run test cases, not included when packaging.




How to use  :

  •   checkout project
  •   Use the ide method to import the maven project. If you need to manually: run ant lib.init (If there is an error when downloading the jar, please check the groupId, artifactId and version number.)
  •   Create the project manually, specify the library, you don't need to find one by one. (If you import the maven project, everything will be configured automatically)
  •  

Guess you like

Origin blog.csdn.net/guoguo527/article/details/53176032