How to use Maven in my Java Project and Why?

前端 未结 7 516
臣服心动
臣服心动 2020-12-24 02:42

I am trying to figure out the use of Maven and I got many articles describing its features and uses. But I am just not able to understand the actual use of Maven from produc

相关标签:
7条回答
  • 2020-12-24 03:16

    Free books about Maven can be downloaded from Sonatype (where the original developers of Maven come from).

    Also see the documentation on the Apache Maven website.

    0 讨论(0)
  • 2020-12-24 03:16

    Where does Maven come into picture? I have used Ant and I understand Ants benefit of a standardized build process. But why do we need an advanced Ant in form of Maven?

    • Maven introduced "convention over configuration" this helps if some colleagues write bigger ant scipts than code. plus dependency management, the only trouble is to convert monolithic projects with many artifacts.

    In any case, I need to use it, so where do I get started - basic flow, some good tutorials?

    • I found these tutorials
    • And Maven: The Definitive Guide

    helpful.

    0 讨论(0)
  • 2020-12-24 03:25

    If you are within an organization, try to build a maven repository proxy. Artifactory is a good option.

    0 讨论(0)
  • 2020-12-24 03:33

    The latest netbeans also has a pretty good maven integration.

    0 讨论(0)
  • 2020-12-24 03:34

    for all those wondering where the maven downloads the dependency jars, check out a folder named .m2 in the user root directory. eg. for me it is the c:\documentsand settings\myUserName.m2\

    also i have researched a bit on maven and i have made some small scribbling like reminders. If it is worth a read then here it is ::

    /* mvn generate mvn install downloads all necessary jars mvn test tests the application made... mvn site builds the site downloading dependencies

    to deploy the site, we need to declare a location to distribute to in your pom.xml, similar to the repository for deployment. ... website scp://www.mycompany.com/www/docs/project/ ...

    mvn site-deploy deploys the site

    how to build structure of site : The site.xml file is used to describe the layout of the site, and replaces the navigation.xml file used in Maven

    A sample is given below:

    Maven http://maven.apache.org/images/apache-maven-project.png http://maven.apache.org/ http://maven.apache.org/images/maven-small.gif

    <menu name="Maven 2.0">
      <item name="Introduction" href="index.html"/>
      <item name="Download" href="download.html"/>
      <item name="Release Notes" href="release-notes.html" />
      <item name="General Information" href="about.html"/>
      <item name="For Maven 1.x Users" href="maven1.html"/>
      <item name="Road Map" href="roadmap.html" />
    </menu>
    
    <menu ref="reports"/>
    
    ...
    

    so in effect, we need to link our html to this structure format to make the website layout also in order for us to add any new css or such stuff, all we need to do is to put them into the resources part of the src folder

    then we can create a war file of our project and lay it out in the httpd folder of apache or such similar folder ofour web server

    In case we need to generate projects, we need to add a few lines of code to our pom.xml file and that is: ... org.apache.maven.plugins maven-project-info-reports-plugin 2.0.1 ...

    also, site descriptors are to be set in site.xml

    the details can be seen in the documentation of maven

    maven structure with their importance:

    project/ pom.xml - Defines the project src/ main/ java/ - Contains all java code that will go in your final artifact.
    See maven-compiler-plugin for details scala/ - Contains all scala code that will go in your final artifact. ////not needed for our current project as of yet See maven-scala-plugin for details resources/ - Contains all static files that should be available on the classpath in the final artifact. See maven-resources-plugin for details webapp/ - Contains all content for a web application (jsps, css, images, etc.)
    See maven-war-plugin for details site/ - Contains all apt or xdoc files used to create a project website.
    See maven-site-plugin for details
    test/ java/ - Contains all java code used for testing.
    See maven-compiler-plugin for details scala/ - Contains all scala code used for testing.
    See maven-scala-plugin for details resources/ - Contains all static content that should be available on the classpath during testing. See maven-resources-plugin for details

    mvn validate this will validate that all the dependencies are satisfied and nothing is missing mvn compile this will compile the project mvn verify checks whether the package is valid or not also in the project, the dependencies are to be inserted into the xml file

    the example of dependencies injection is given below:: org.scala-lang scala-library 2.7.2-rc2 junit junit 3.8.1 test

    Each dependency consists of several items:

    * groupId - The group of the dependency to rely on
    * artifactId - The artifact in the group to rely on
    * version - The version of the dependency to rely on
    * scope - The "scope" of the dependency. Defaults to compile (more details later)
    * packaging - The packaging for the dependency.  Defaults to jar (e.g. jar, war, ear)
    

    You can integrate your static pages by following these steps:

    * Put your static pages in the resources directory, ${basedir}/src/site/resources
    * Create your site.xml and put it in ${basedir}/src/site
    * Link to the static pages by modifying the menu section, create items and map them to the filenames of the static pages
    

    mvn tomcat:deploy to deploy to tomcat or apache, you can go for this command

    0 讨论(0)
  • 2020-12-24 03:42

    I had exactly the same perception as you and for years I avoided Maven.

    The thing is, it allows you to easily get the required jars your application may need( called dependencies - jars and other things - ) . So the next time somebody else run your project he will get the jars automatically.

    I know that's a bit hard to grasp, until you work with an existing projects using it.

    For instance I downloaded an open source project recently, which depended on 10 or 12 different on different jar versions. After downloading the source code and executing Maven, all those jars ( and a lot more others ) were downloaded for me.

    The problem with Maven ( as a friend of mine told me ) is that to perform a "Hello world" program, it first downloads the world to greet him. :P

    0 讨论(0)
提交回复
热议问题