Using Maven 3 with Eclipse to handle your dependencies (with screenshots)

Discussion in 'Resources' started by bigteddy98, Jan 5, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    bigteddy98

    Hello everybody,

    After I heard many people talking about Maven, I decided to write a tutorial on how use Maven in Eclipse. I hope you enjoy reading and I would love to see it back in your projects.

    What is Maven?
    Maven is an easy tool to handle the dependencies and resources of your Java projects. Most times it's used to handle the dependencies and resources of programms (such as CraftBukkit), but I found out how to use it for your Bukkit plugins. Normally, you add your dependencies as external libraries to your Java project in eclipse, but in Maven, that works with a POM file. A POM file is a file included in your plugins which includes the plugins information (such as the name, the version and ofcourse the dependencies). This can be very easy when your work with a team on a project. With Maven you can also setup a Jenkins server. I will create a tutorial for that as soon as possible.

    Using Jenkins with Maven
    Some of you might know Jenkins. Jenkins is a Java service which automatically builds a new jar file when someone pushes an update to your Git. If you want to, you can automatically put this jar file available in your build list. You can put your build server IP on your BukkitDev page, so people will be able to download Development builds (Ofcourse at their own risk because the BukkitDev MODs/Admins can't control what you upload).
    [mod edit - removed unofficial builds link]

    Lets start converting our project

    Step 1 - Converting our project.
    Converting your plugin to a Maven plugin in Eclipse is very easy, right click your project -> Configure -> Convert to Maven Project.

    [​IMG]
    After you've done that, a window like this will show up:
    [​IMG]
    Here we are going to give the basic plugins information. Important: your Group Id and Artifact Id may not contain any spaces. So in my case, i have to change "Example Project" to "ExampleProject". I advise you to keep the Group Id and the Artifact Id the same. The Version is simpely the version, in my case that's 0.0.1. Click finish and a POM.xml file will show up in your project. This will look like this:

    [​IMG]

    Now we are going to edit our POM.xml, I usually do that with the eclipse text editor.
    [​IMG]

    In my case it looks like this:
    PHP:
    <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>ExampleProject</groupId>
      <
    artifactId>ExampleProject</artifactId>
      <
    version>0.0.1</version>
      <
    build>
        <
    sourceDirectory>src</sourceDirectory>
        <
    plugins>
          <
    plugin>
            <
    artifactId>maven-compiler-plugin</artifactId>
            <
    version>3.1</version>
            <
    configuration>
              <
    source>1.7</source>
              <
    target>1.7</target>
            </
    configuration>
          </
    plugin>
        </
    plugins>
      </
    build>
    </
    project>
    Step 2 - Adding plugin.yml to your POM.
    A very important thing of your plugin is the plugin.yml, so we have to tell Maven that it has to include the plugin.yml file when building. We include the plugin.yml like this:
    PHP:
          <resources>
                <
    resource>
                    <
    filtering>true</filtering>
                    <
    directory>${basedir}</directory>
                    <
    includes>
                        <include>*.
    yml</include>
                    </
    includes>
                </
    resource>
            </
    resources>
    The resource is a part of the build information, so I will put it before we close the build with </build>

    Now our pom.xml looks like this:
    PHP:
    <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>ExampleProject</groupId>
      <
    artifactId>ExampleProject</artifactId>
      <
    version>0.0.1</version>
      <
    build>
        <
    sourceDirectory>src</sourceDirectory>
        <
    plugins>
          <
    plugin>
            <
    artifactId>maven-compiler-plugin</artifactId>
            <
    version>3.1</version>
            <
    configuration>
              <
    source>1.7</source>
              <
    target>1.7</target>
            </
    configuration>
          </
    plugin>
        </
    plugins>
     
          <
    resources>
                <
    resource>
                    <
    filtering>true</filtering>
                    <
    directory>${basedir}</directory>
                    <
    includes>
                        <include>*.
    yml</include>
                    </
    includes>
                </
    resource>
            </
    resources>
     
      </
    build>
    </
    project>
    Step 3 - Adding CraftBukkit to your dependencies.
    Now we are almost done, the only thing we have to do is tell the POM file where it can find the CraftBukkit dependency. Open the POM file with your text editor again and add the information of your dependencies. This is how it looks for me (only the CraftBukkit 1.7.2-R0.1 build as dependency):

    PHP:
    <repositories>
            <
    repository>
              <
    id>bukkit-repo</id>
              <
    url>http://repo.bukkit.org/content/groups/public/</url>
            
    </repository>
    </
    repositories>
    <
    dependencies>
            <
    dependency>
                <
    groupId>org.bukkit</groupId>
                <
    artifactId>bukkit</artifactId>
                <
    version>1.7.2-R0.1</version>
            </
    dependency>
    </
    dependencies>

    The dependencies aren't part of the build. The dependencies are part of the project, so we put it before we close the project with </project>. Now our POM.xml looks like this (this is how your POM file should be now):

    PHP:
    <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>ExampleProject</groupId>
      <
    artifactId>ExampleProject</artifactId>
      <
    version>0.0.1</version>
      <
    build>
        <
    sourceDirectory>src</sourceDirectory>
        <
    plugins>
          <
    plugin>
            <
    artifactId>maven-compiler-plugin</artifactId>
            <
    version>3.1</version>
            <
    configuration>
              <
    source>1.7</source>
              <
    target>1.7</target>
            </
    configuration>
          </
    plugin>
        </
    plugins>
     
          <
    resources>
                <
    resource>
                    <
    filtering>true</filtering>
                    <
    directory>${basedir}</directory>
                    <
    includes>
                        <include>*.
    yml</include>
                    </
    includes>
                </
    resource>
            </
    resources>
     
      </
    build>
     
          <
    repositories>
            <
    repository>
              <
    id>bukkit-repo</id>
              <
    url>http://repo.bukkit.org/content/groups/public/</url>
            
    </repository>
          </
    repositories>
          <
    dependencies>
            <
    dependency>
                <
    groupId>org.bukkit</groupId>
                <
    artifactId>bukkit</artifactId>
                <
    version>1.7.2-R0.1</version>
            </
    dependency>
          </
    dependencies>
     
    </
    project>
    If you want to know which CraftBukkit builds are currently available, check out this page:
    http://repo.bukkit.org/content/groups/public/org/bukkit/bukkit/

    Check if it works
    If you want to know if you did everything correctly, I have two steps for you:
    1. Check if your POM.xml looks like this in your eclipse:
    [​IMG]
    If you have a red X, you did something wrong. Please check the steps above again to be sure you did everything correctly.
    2. Try creating a class and let it extend JavaPlugin. If you are able to import JavaPlugin, you know you did everything right. If not, please check the steps above again to be sure you did everything correctly.

    Now push it to your Git and you can start teaming easily on your project with Maven. The main idea behind this is to be able to build with Jenkins, which I already explained at the top of this page. I will post a tutorial how to setup that as fast as possible.

    If you found and grammar mistakes, tell me please :) I am dutch.
     
  2. Offline

    maxlehot1234

    NO.... Is it possible to make a tutorial on how to use SOURCE CODE from bukkit plugin, edit it into eclipse, add dependancies, add bukkit build and how to export it to use it on my server...If you can make this, it is a just amazing, because i search for a long time, but i dont find anything...
     
  3. Offline

    bigteddy98

    Could do that if you want, think it's a pretty good idea. Maybe I will make something like that.

    EDIT:
    It's not gonna happend, not allowed to post a tutorial I think:
    [​IMG]
     
    maxlehot1234 likes this.
  4. Offline

    maxlehot1234

    I really need your help with this and i think a lot of people want this tutorial ;)

    I try to import github project but it is not working, i have:

    this.PNG

    Is it normal? i have orange icon on each package?
     
  5. Offline

    bigteddy98

    That means you are connected to the git, try rightclick -> team -> disconnect or something to disconnect.
     
    maxlehot1234 likes this.
  6. Offline

    maxlehot1234

    Oh tahnk you :p And ihow i can change lib folder? I see plugins into lib folder, but i want to put the updated plugin in lib folder...

    I see world edit 5.4.6, but i want to replace it by: world edit 5.5.8 nd change craftbukkit 1.4.7 to craftbukkit 1.6.4, it is possible?


    EDIT:I found the solution :p

    If i want to export it and use it onmy server, i simply need to do: export>java>jar file
    and thats it?
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    maxlehot1234
    The orange icon is normal for projects that are version controlled by git.

    I wrote this a while ago explicitly targeting getting maven projects into eclipse from github
    https://docs.google.com/document/d/1lIpbUid3yTTbebYeVFRJWYNLxwWuu7HprocRFTKRGb8/edit?usp=sharing

    The project in your screenshot however is using ant, so it takes a little bit of setup to get eclipse to use the ant build.xml

    maxlehot1234 the .project file is how eclipse tracks that the current directory is an Eclipse project, if it is not present in the Navigator (by default the package explorer hides it) then the directory is not an Eclipse Project.

    As for your actual question, you need to read the second page. You project does not have a pom.xml thus you need to manually add the Java Nature to a general project, if you imported as such. During the import process, it gave you a chance to import projects, by I've never found it to actually work out properly for a few versions (i.e. without clobbering some tracking state).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
    maxlehot1234 likes this.
  8. Offline

    maxlehot1234


    Thank you for your help, i test it on my server soon. I follow your link information and i see pom.xml now ;) :) So i export the project...my hosting have problem for the moment, but i write here if i have problem, is it good?
     
  9. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Removed links to unofficial builds.
     
  10. Offline

    maxlehot1234

    What?

    I just want to say: i see 1 warning into eclipse...
    This is the warning i have:
    DescriptionResourcePathLocationType
    Build path specifies execution environment J2SE-1.5. There are no JREs installed in the workspace that are strictly compatible with this environment.PrisonSuiteBuild pathJRE System Library Problem
     
  11. Offline

    bigteddy98

    That's a possiblity, you could also install it using Maven.

    Thanks for that, saved to my desktop :D.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  12. Offline

    maxlehot1234


    I need to wait before testing, because my server hosting have a lot of problem with VPS at the moment...I give the result on this subject after the test ;)
     
  13. Offline

    maxlehot1234

    Hi, i just want to know, how to add dependancies, like: craftbukkit, because, when i add it and export the jar file, my jar file, make 20 MB ... I know it is not normal, do you have a solution or i make something wrong?
     
  14. Offline

    bigteddy98

    Do not export it as you usually do, install it with Maven.

    EDIT:
    It should also work when normally exporting, what are your export settings?
     
  15. Offline

    maxlehot1234

    What you want to say about export setting?

    How can i install it with maven? I want to export it from eclipse and put it in my plugin folder...
     
  16. Offline

    bigteddy98


    You can build it with Maven, that's what I tried to say. You can also just export it from eclipse, but be sure not to include your libraries (which you probebly did).
     
  17. Offline

    Aqgorn

    I'm getting an error on <repository>, trying to import the Fanciful lib, help anyone?
    Error:
    Show Spoiler
    cvc-complex-type.2.4.a: Invalid content was found starting with element 'repository'. One of '{"http://
    maven.apache.org/POM/4.0.0":scriptSourceDirectory, "http://maven.apache.org/POM/
    4.0.0":testSourceDirectory, "http://maven.apache.org/POM/4.0.0":eek:utputDirectory, "http://maven.apache.org/
    POM/4.0.0":testOutputDirectory, "http://maven.apache.org/POM/4.0.0":extensions, "http://maven.apache.org/
    POM/4.0.0":defaultGoal, "http://maven.apache.org/POM/4.0.0":resources, "http://maven.apache.org/POM/
    4.0.0":testResources, "http://maven.apache.org/POM/4.0.0":directory, "http://maven.apache.org/POM/
    4.0.0":finalName, "http://maven.apache.org/POM/4.0.0":filters, "http://maven.apache.org/POM/
    4.0.0":pluginManagement}' is expected.
     
  18. Offline

    maxlehot1234

    I need to do this? Is it work if i unchecked the lib ?

    likethis.PNG
     
  19. Offline

    bigteddy98

    Hmm, try it, I think it will work without, don't know where you're using the lib folder for.
     
  20. Offline

    Sagacious_Zed Bukkit Docs

    If you have a maven project, you should be letting maven build your project for you.
    By default running
    Code:
    mvn package
    will generate the jar with the classes compiled and the resources included in the package . If you require more than that, such as including dependencies, you must specify what to do in the pom.xml

    In eclipse there is a Run as maven option. However, there is no default mvm package, and but if you don't want to set it up, you can run mvn install instead.
     
  21. Offline

    bigteddy98

    Thanks for explanation, didn't really know how to explain it. :).
     
  22. Offline

    Europia79

    Thanks for the tutorial !
     
Thread Status:
Not open for further replies.

Share This Page