[Tutorial] Setting up Maven

Discussion in 'Resources' started by zeeveener, Dec 9, 2013.

?

Was this Helpful? (Non-Public Results)

  1. Yes, very much so!

    40 vote(s)
    74.1%
  2. Yes, a bit.

    11 vote(s)
    20.4%
  3. No, not really.

    1 vote(s)
    1.9%
  4. No, not at all!

    2 vote(s)
    3.7%
Thread Status:
Not open for further replies.
  1. Offline

    zeeveener

    Beware: You are entering a Wall of Text. I tried to format it as best I could, but it is a lot of information and it can get hypnotic at places. Bare with it and you will be fine.

    So, as we all know, setting up a template for easily starting new Bukkit plugins can be a tedious process. You have to make the project/module in your IDE, then you need to go and download your planned dependencies from their respective repos and place them into an easily accessible folder. THEN you are forced to figure out the Build Path system in your IDE.

    Don't get me wrong, when you get this down to a system, it's pretty simple, but still tedious. Why would I want to store dependencies on my computer? I'd rather it was always up to date without me downloading anything!

    Well, that's where Maven comes in. Simply put, Maven is a powerful project management tool that allows you to automate many of the individual processes involved with any Java project.

    It can:
    • Download and reference external dependencies
    • Deploy to CI or Repos or both
    • Much more that I am not familiar with :)
    This post will be an attempt to help explain some of the process of making use of Maven in your Bukkit project. It assumes you have Maven 3.0+ installed and properly setup on your computer. (M2_HOME set on Linux/Unix Systems, etc.)

    If you are unsure or haven't installed it, follow the directions near the bottom of this website. (They are VERY EASY) http://maven.apache.org/download.cgi
    Anyways, let's get started.



    Setting Up A Project
    Everybody has personal preferences regarding which IDE is best or their favourite. Personally, I started with Eclipse, but I have recently been trying to migrate over to IntelliJ IDEA for speed reasons. However, I will attempt to explain each step as it works for the three main IDEs (Eclipse, Netbeans, IDEA). If I get anything wrong, please let me know so that I can make the changes.




    Creating the Maven Project
    Using Eclipse, there is a little bit of setup required before it will recognize and use Maven.
    1. Open the Eclipse Marketplace.
    2. Search for Maven Integration
    3. There will be 2 versions. Unless your version of Eclipse is ancient, get the 1.4 version (the one that doesn't have WTP in the title)
    4. Install that and restart Eclipse.
    5. Now, go to File -> New -> Project...
    6. In the menu, choose Maven -> Maven Project. Click Next.
    7. If you wish to get fancy with the default template, click next again.
    8. Otherwise, choose the "Create Simple Project" checkbox and click Next
    9. The GroupID would be one of the following:
      1. If you own a domain: com.domain.optional (Obviously com could be org/net/ca/etc. The optional is if you have many projects that have similar functions and wish to categorize them further)
      2. If you do not own a domain: me.username
      3. These are conventions. Please follow them so that you reduce the risk of conflicts with other programs
    10. The ArtifactID is the Name of your project. Some people say to use lowercase, I don't. I don't know how strong of a convention that is so I ignored it lol.
    11. Version is obvious.
      1. The SNAPSHOT suffix is important for when you have a repo that you deploy to. If you don't have a repo or CI, you can just enter a number like normal.
    12. Name is the same as the artifact, but definitely uses capital letters if required.
    13. Description is a simple description of the plugin.
    14. The Parent Project section can be ignored if you don't know how to handle it. It's not required for Bukkit plugins.
    15. Click finish. Your project is now ready to go!
    Note:If you have a plugin already and you wish to make it a maven plugin, simply follow steps 1-4. Then right click on your project, Configure -> Convert to Maven Project. You will have to fill out the same information as the above process.

    If you use IDEA or Netbeans, you have it much simpler.
    • Netbeans
      1. File -> New Project...
      2. Choose Maven -> Java Application
      3. Fill out the information. The names are all the same as above so you can read the Eclipse explanations.
      4. Click finish. That's it!
    Note:If you have a plugin already and you wish to make it a maven plugin, you will still need to follow the same steps to create a new project. You will then have to copy the source files from the original project into the new project.
    • IntelliJ IDEA
      1. Click on Create New Project in the main window.
      2. Select Maven and fill in the information. Click Next
      3. Fill in the same information as the Eclipse section.
      4. You can safely ignore the "Create from Archetype" option and click finish.
      5. Done.
    Note:If you already have a project and wish to make it a maven plugin, right click on the project and select "Add Framework Support..." Select Maven and then Finish. Bam. You have a Maven project. You will have to edit the pom.xml to include the groupId and artifactId information which aren't set yet.

    Hopefully this section wasn't TOO hard for you. It is really simple when you think about it.


    Adding Default Dependencies


    Now that you have a project, you may be excited to start coding right away. Hold your horses there Silver, we need to setup the dependencies first. Otherwise, you will be running into problems left, right and centre.

    To do this, we will edit the pom.xml file directly. I am sure there are easier ways to do this, but by editing the file directly, you are getting familiar with the pom which can come in handy later on down the road.

    In all three IDEs, you will see a file called "pom.xml" Click to open the file. Eclipse might show you a page with clickable options and text fields, just click "pom.xml" at the bottom to get the text view which is what we want.

    If you are unfamiliar with xml formatting, basically you have sections which are named within the "<>" brackets. There is a closing tag with a "/" in front of the tag word. The options/values for that section go between the opening and closing tag.

    <tag>*OPTIONS*</tag>

    In the following steps, you ignore the quotations surrounding the values when entering them into the pom.xml. They are simply there for formatting reasons.
    • You will want to create a section called "repositories".
    • Within that section create another called "repository".
    • Within that we will define where the bukkit repo is. Add a section called "id" and enter "bukkit-repo" as the value.
    • Add another section in "repository" called "url" and "http://repo.bukkit.org/content/groups/public/" as the value.
    • Now, underneath the "repositories" section, create one called "dependencies".
    • Within "dependencies" add a section called "dependency"
    • Within "dependency" add a section called "groupId" if it wasn't added for you.
      • Enter "org.bukkit" as the value
    • Within "dependency" add the "artifactId" section
      • Enter "bukkit" as the value
    • Also within "dependency" add the "version" section.
      • Add the most recent version of bukkit. At the time of this post, it is "1.7.2-R0.1-SNAPSHOT".
        • I have been notified that some computers may have problems with SNAPSHOTs. There is a setting somewhere in your Maven that would enable them Google it.
        • Also, if it works for you, you can use "LATEST" to always get the latest non-snapshot version of the dependency.
      • You can determine this by going to http://repo.bukkit.org/content/groups/public/org/bukkit/bukkit/ and looking for the latest version.
    Show Spoiler
    Here it is in a more copy-pasteable format... Remove the square bracketed URL formatters. They won't go away. You want the <url></url> though.
    Code:xml
    1.  
    2. <repositories>
    3. <repository>
    4. <id>bukkit-repo</id>
    5. <url>[url]http://repo.bukkit.org/content/groups/public/</url>[/url]
    6. </repository>
    7. </repositories>
    8.  
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.bukkit</groupId>
    12. <artifactId>bukkit</artifactId>
    13. <version>1.7.2-R0.1-SNAPSHOT</version>
    14. </dependency>
    15. </dependencies>
    16.  


    Save the pom.xml and now you can safely start programming a Bukkit plugin.


    Compiling a Bukkit Plugin using Maven


    Eventually, you are going to want to build and test your plugin. This is also handled using the Maven build process. Don't worry, it's super simple.


    Pom.xml Setup
    • In pom.xml, you will want to create a section called "build".
      • There are a lot of options you can use in this section to configure your build to exactly how you want it. However, I am going to give you a very simple setup that simply compiles the plugin for you. For more options, check out http://maven.apache.org/pom.html#Build_Settings
    • Within "build", make a section called "finalName".
      • Enter the core name of your project. Otherwise, maven will append the version to the end of the jar name. (Example: ZPort.jar compared to ZPort-8.3-SNAPSHOT.jar)
    • Within "build", make a section called "sourceDirectory"
      • Enter the path to the java folder in your source tree.
      • By default, the value to be entered would be "src/main/java"
    • Within "build", make a section called "resources"
    • Within "resources", make a section called "resource"
    • Within "resource", make the sections "targetPath", "filtering", "directory", "includes"
    • Within "targetPath", enter the path to the resources folder in your source tree
      • This can usually be a simple period. "."
    • Within "filtering", enter the value "true"
    • Within "directory", enter the value "${basedir}/src/main/resources/"
    • Within "includes", make a section called "include"
      • Enter the value "plugin.yml"
      • If you have a config.yml in your resources folder, simply make another "include" within the "includes" and enter "config.yml" as the value
    Now, the next part isn't necessarily required, but it is very helpful in preventing errors in my plugins so I might as well give it to you as well.
    • Within "build", make a section called "plugins"
    • Within "plugins", make a section called "plugin"
    • Within "plugin", make the sections "artifactId", "version", and "configuration"
    • Within "artifactId", set the value to "maven-compiler-plugin"
    • Within "version", set the value to "2.3.2"
    • Within "configuration", make the sections "source" and "target"
    • Within both "source" and "target", set the value to "1.6"
    I'm not exactly sure what this does, but I believe it forces the compiled jar to use Java 1.6 which makes sure it will run on most every computer using Bukkit. (Not everyone has installed 1.7 yet. Slow pokes...)

    Show Spoiler
    This is the culmination of the above steps. Remove the square bracketed URL formatters. They won't go away. You want the <url></url> though.
    Code:xml
    1.  
    2. <build>
    3. <finalName>ZPals</finalName>
    4. <sourceDirectory>src/main/java</sourceDirectory>
    5. <resources>
    6. <resource>
    7. <targetPath>.</targetPath>
    8. <filtering>true</filtering>
    9. <directory>${basedir}/src/main/resources/</directory>
    10. <includes>
    11. <include>plugin.yml</include>
    12. </includes>
    13. </resource>
    14. </resources>
    15. <plugins>
    16. <plugin>
    17. <artifactId>maven-compiler-plugin</artifactId>
    18. <version>2.3.2</version>
    19. <configuration>
    20. <source>1.6</source>
    21. <target>1.6</target>
    22. </configuration>
    23. </plugin>
    24. </plugins>
    25. </build>
    26.  



    Build Settings
    • In Eclipse
      1. Right click on the project and select, Run As -> Maven Build... (Make sure it's the "..." one)
      2. In Goals, type "clean package".
        1. There are a whole bunch of options for building a Maven project, but they are out of the scope of this tutorial.
      3. Click Apply.
      4. Click Run.
      5. The console will start spewing out useless info. You are looking for the bottom to say "Build Successful". If it's a Failure, there should be a simple explanation which you can post here and get help or google. (Google might be a better option with more immediate answers)
      6. In the future, you can simply click on the Run icon at the top and it should run the proper Build Process as long as you have the right project selected in the workspace.
    • In Netbeans
      1. In the little drop down where you choose which profile to choose, click on Customize...
      2. Go to the Run section in the menu.
      3. In arguments, enter "clean package"
      4. Click ok.
      5. Now Build the project. Simply running it will only give you problems. You want to build the jar.
      6. Look for the Build Successful at the bottom. If it's a failure, read step 5 of the Eclipse section.
    • In IntelliJ
      1. In the Main Menu, click Run -> Edit Configurations
      2. Click on the little plus sign at the top left and select Maven
      3. You can give it a useful name if you like
      4. In Command Line, enter "clean package"
      5. Click Apply then Ok.
      6. Now, go to Run -> Run "NAME". If it's the wrong name, choose Run... and select the right profile.
      7. You will want to see the Build Successful message. Otherwise, look at step 5 in the Eclipse section.
    If all went well, you should now have a folder within your project folder called target. Within that is the jar file that is your plugin!

    You can now copy the plugin over to the plugins/ folder in your Bukkit Server and start it up like a normal plugin.

    Hopefully this was helpful for you. If not, well, there are a ton of tutorials online regarding setting up a simple Maven project. You will figure it out!

    If you would like to see/use a very simple pom.xml that I have set up as an example, you can go to https://github.com/zeeveener/SimpleBukkitPom/blob/master/pom.xml and look at that. If you choose to use it, make sure you make the necessary changes to make it your pom.xml and not use my name, project name, etc.
     
  2. Offline

    JOPHESTUS

    DSH105 and zeeveener like this.
  3. Offline

    macguy8

    I've got almost none of the build -> resources section in all my other projects. Any reason why it's better to have it there?
     
  4. Offline

    viper_monster

    zeeveener, ummm, do you maybe know what is wrong here:
    PHP:
            <dependency>
                <
    groupId>com.sk89q</groupId>
                <
    artifactId>worldguard</artifactId>
                <
    version>5.8</version>
                <
    scope>compile</scope>
                <
    type>jar</type>
            </
    dependency>
     
            <
    dependency>
                <
    groupId>com.sk89q</groupId>
                <
    artifactId>worldedit</artifactId>
                <
    version>5.5.8</version>
                <
    scope>compile</scope>
                <
    type>jar</type>
            </
    dependency>
     
  5. Offline

    zeeveener

    macguy8

    Well, I have it there to ensure that no files that I don't want in my plugin are there. It allows me complete control over how my plugin is packaged. I'm sure you could remove some, or maybe all of it and it would still work.

    spoljo666

    What exactly is the problem?

    Make sure the <repository> is set properly for sk89qs repo. Also, try using the latest version with a number in the name. See if that helps.
     
  6. Offline

    viper_monster

    zeeveener, the problem is that the versions are incorrect/red:
    [​IMG]
     
  7. Offline

    zeeveener

    spoljo666

    Make sure you have the repository section set up properly.
    What does your repository section look like for sk89qs repo?

    I would do it like this:
    For whatever reason, this forum won't remove the square bracket URL things.. Those are NOT supposed to be there, only the <url></url> are supposed to be there.

    Code:xml
    1.  
    2. <repositories>
    3. <repository>
    4. <id>sk89q-repo</id>
    5. <url>[url]http://maven.sk89q.com/repo/</url>[/url]
    6. </repository>
    7. </repositories>
    8. <dependencies>
    9. <dependency>
    10. <groupId>com.sk89q</groupId>
    11. <artifactId>worldguard</artifactId>
    12. <version>5.8.1-SNAPSHOT</version>
    13. </dependency>
    14. <dependency>
    15. <groupId>com.sk89q</groupId>
    16. <artifactId>worldedit</artifactId>
    17. <version>5.5.8-SNAPSHOT</version>
    18. </dependency>
    19. </dependencies>
    20.  


    You can see the repo by going to http://maven.sk89q.com/repo
    Ignore the obvious world guard and world edit sections, you want to follow the artifactId by going through com -> sk89q -> worldguard/worldedit.
     
    spoljo666 likes this.
  8. Offline

    viper_monster

    zeeveener, hmmm it still does the same thing:[​IMG]
     
  9. Offline

    zeeveener

    spoljo666

    What does it say when you hover over to get the error message?
     
  10. Offline

    viper_monster

    Last edited by a moderator: Jun 5, 2016
    zeeveener likes this.
  11. Offline

    zeeveener

    spoljo666

    Go back to the Build Settings section and remove the "clean" option. For whatever reason it seems to be unable to delete a file. You could check the permissions on that file as well, but I am not familiar with Windows so I can't help you with that.
     
    spoljo666 likes this.
  12. Offline

    BungeeTheCookie

    zeeveener likes this.
  13. Offline

    L33m4n123

    Cheers. That is like the best readable and easiest to understand tutorial about Maven I found so far.

    However what you may need to know

    In order for this to work you would need to enable somewhere within Maven to use Snapshots otherwise it won't work.
    What might be interresting to notice up there aswell, if you always want to use at least the latest Beta build [they are not snapshots]
    use "LATEST" this will always use the newest dependency
     
  14. Offline

    zeeveener

    L33m4n123

    I have never changed a setting for Allowing Snapshots so it's default on my machine.
    Also, I have tried LATEST, and it was throwing errors left and right. So, I suppose, to each their own.
     
  15. Offline

    L33m4n123


    ok. Yeah Snapshot was throwing me errors left and right and after googeling I figured out that by default it is disabled. strange^^
     
    zeeveener likes this.
  16. Offline

    zeeveener

    L33m4n123

    Seems as though Maven isn't installed consistently across computers. Or perhaps it's the IDE messing about. IDK lol
     
  17. Offline

    jorisk322

    Thanks! This was very helpful. I have a problem though: I use IJ and it says I haven't set the M2_HOME environment variable, which is not true. I'm using Windows 7 (Ultimate). I did get it working by overriding the variable in my IJ config, but I would rather just get the other method working. Any idea what the problem might be?
     
  18. Offline

    L33m4n123


    sure your M2_HOME is set correctly? works perfectly fine for me
     
  19. Offline

    jorisk322

    mvn --version shows the value of the variable just fine.
     
  20. Offline

    nitrousspark

    it appears it does not want to work for me, my M2_HOME is set up (i can type mvn --version and it shows stuff lol) it gives me a MojoFailureException and googling it i got mixed answers

    error:
    Code:
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    [INFO] Scanning for projects...
    [INFO]                                                                 
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Minigames 0.0.1
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ Minigames ---
    [INFO] Deleting C:\Users\nitrousspark\workspace\Minigames\target
    [INFO]
    [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ Minigames ---
    [debug] execute contextualize
    [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
    [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 1 resource to .
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Minigames ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
    [INFO] Compiling 9 source files to C:\Users\nitrousspark\workspace\Minigames\target\classes
    [INFO] -------------------------------------------------------------
    [ERROR] COMPILATION ERROR :
    [INFO] -------------------------------------------------------------
    [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    [INFO] 1 error
    [INFO] -------------------------------------------------------------
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.628s
    [INFO] Finished at: Tue Feb 25 22:23:47 PST 2014
    [INFO] Final Memory: 8M/205M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Minigames: Compilation failure
    [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    [ERROR] -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
    
    pom.xml:
    Code:
    <project xmlns="[URL]http://maven.apache.org/POM/4.0.0[/URL]" xmlns:xsi="[URL]http://www.w3.org/2001/XMLSchema-instance[/URL]" xsi:schemaLocation="[URL]http://maven.apache.org/POM/4.0.0[/URL] [URL]http://maven.apache.org/xsd/maven-4.0.0.xsd[/URL]">
    <modelVersion>4.0.0</modelVersion>
    <groupId>me.nitrousspark</groupId>
    <artifactId>Minigames</artifactId>
    <version>0.0.1</version>
    <build>
    <finalName>Minigames</finalName>
    <sourceDirectory>src</sourceDirectory>
    <resources>
    <resource>
    <targetPath>.</targetPath>
    <filtering>true</filtering>
    <directory>${basedir}/src/main/resources/</directory>
    <includes>
    <include>plugin.yml</include>
    </includes>
    </resource>
    </resources>
    <plugins>
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
    <source>1.7</source>
    <target>1.7</target>
    </configuration>
    </plugin>
    </plugins>
    </build>
    <repositories>
    <repository>
    <id>bukkit-repo</id>
    <url>[URL]http://repo.bukkit.org/content/groups/public/</url>[/URL]
    </repository>
    </repositories>
    <dependencies>
    <dependency>
    <groupId>org.bukkit</groupId>
    <artifactId>bukkit</artifactId>
    <version>1.7.2-R0.4-SNAPSHOT</version>
    </dependency>
    </dependencies>
    </project>
     
  21. Offline

    xTrollxDudex

    nitrousspark
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Minigames: Compilation failure
    [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

    Have you set your Java path yet? If so, Make sure it is a JDK install, not Java Runtime.
     
  22. Offline

    nitrousspark

    xTrollxDudex
    C:\Program Files (x86)\Java\jdk1.7.0_09 but does it matter if there is also a C:\Program Files (x86)\Java\jre7?
     
  23. Offline

    xTrollxDudex

    nitrousspark
    Is that actually a legitimate copy of JDK? I don't remember ever seeing my JDK versioned like that, ever. Also, make sure that it is JAVA_HOME. Did you spell the path correctly?
     
  24. Offline

    nitrousspark

  25. Offline

    xTrollxDudex

  26. Offline

    nitrousspark

    xTrollxDudex
    im really unsure about this one, but this is what it shows up in preferences
    http://i.imgur.com/b0covth.png

    and now its showing errors in my main class that shouldn't be errors. like importing a class in another package is an error and it says i cannot be resolved :/
     
  27. Offline

    Europia79

    Oh god, i've been messing around with Maven for the past two weeks and I wish I would have found this thread first. Oddly, google didn't give me this thread until i did:
    1st problem I had was converted a Netbeans Ant project to a Maven project. Here is an alternative way to convert:
    1. Open Netbeans, close the project you want to convert, create a new Maven project
    2. Using the Windows, Linux, or Mac filesystem, copy the pom.xml over to the project folder that you want to convert
    3. Edit the pom.xml. Here is a good template to modify: (Change the groupId, artifactId, version, name, and don't forget to goto the build section and change sourceDirectory and resource section so maven includes your plugin.yml
    pom.xml template (open)

    HTML:
    <?xml version="1.0"?>
    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.yourdomain</groupId>
        <artifactId>projectNameHere</artifactId>
        <packaging>jar</packaging>
        <version>172${maven.build.timestamp}</version>
        <name>projectNameHere</name>
        <properties>
            <maven.build.timestamp.format>yyMMdd-HHmm</maven.build.timestamp.format>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        <url>http://maven.apache.org</url>
        <repositories>
            <repository>
                <id>bukkit-repo</id>
                <url>http://repo.bukkit.org/content/groups/public</url>
            </repository>
            <repository>
                <id>sk89q-repo</id>
                <url>http://maven.sk89q.com/repo/</url>
            </repository>
        </repositories>
        <dependencies>
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>bukkit</artifactId>
                <version>1.7.2-R0.3</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.sk89q</groupId>
                <artifactId>worldguard</artifactId>
                <version>5.8.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
        <build>
            <finalName>${artifactId}${project.version}</finalName>
            <extensions>
                <extension>
                    <groupId>org.apache.maven.wagon</groupId>
                    <artifactId>wagon-ssh</artifactId>
                    <version>2.4</version>
                </extension>
            </extensions>
            <defaultGoal>package</defaultGoal>
            <!-- Where our source directory is -->
            <sourceDirectory>${basedir}/src</sourceDirectory>
            <!-- Include the plugin.yml -->
            <resources>
                <resource>
                    <targetPath>.</targetPath>
                    <filtering>true</filtering>
                    <directory>${basedir}/src</directory>
                    <includes>
                        <include>*.yml</include>
                    </includes>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.2</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <minimizeJar>true</minimizeJar>
                                <createDependencyReducedPom>false</createDependencyReducedPom>
                                <relocations>
                                    <relocation>
                                        <pattern>org.example.library</pattern>
                                        <shadedPattern>com.yourdomain.org.example.library</shadedPattern>
                                        <excludes>
                                            <exclude>org.example.library.WeDoNotWantThisClass.*</exclude>
                                        </excludes>
                                    </relocation>
                                </relocations>
                                <filters>
                                    <filter>
                                        <!-- Get the groupID and artifactID from -->
                                        <!-- META-INF/maven/library/pom.properties -->
                                        <artifact>org.groupID:artifactID</artifact>
                                        <includes>
                                            <!-- Use slashes here instead of peroids -->
                                            <include>org/example/library/WeWantThisClass.*</include>
                                            <include>org/example/library/**/*</include>
                                        </includes>
                                        <excludes>
                                            <exclude>org/example/library/WeDoNoTWantThisClass.class</exclude>
                                        </excludes>
                                    </filter>
                                </filters>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>

    4. For Netbeans, rename /nbproject/ to something else, like /nbprojectOLD/ (or delete it)
    5. rename build.xml to something else, like build.xml.txt (or delete it)
    6. open Netbeans. The icon for your project should have changed to a Maven icon. Test with the build button.
    Note: Converting to Maven was the easy part. The hard part was getting the Netbeans build button to work: It didn't work because my pom.xml was missing the maven-compiler-plugin (so don't forget to include that if you want to build with your IDE). Thanks to Alshain01 who helped solve this issue: http://forums.bukkit.org/threads/build-problems-after-converting-to-maven.252425/
    The other problem that I had with Maven was the maven-shade-plugin... So I included that into the template (in case anyone wants to use it to include a library directly into their plugin). Thanks to desht who solved this issue by suggesting to change the dependency section from "provided" scope to "compile" scope for libraries: http://forums.bukkit.org/threads/maven-shading-with-mcstats.196982/#post-2401816
     
  28. Offline

    tcvs

    How would I go about having the info be replaced in the plugin.yml I know you can do ${project.version} and when the jar is being built then it is replaced with the version.
     
  29. Offline

    desht

    I do this in all my plugins, e.g. see https://github.com/desht/ScrollingMenuSign/blob/master/pom.xml and https://github.com/desht/ScrollingMenuSign/blob/master/src/main/resources/plugin.yml

    Steps:
    1. In your plugin.yml, replace any values you want substituted with ${maven-variable-name}. E.g. I use ${project.name} and ${project.version} to automatically insert the name and version from my pom.xml
    2. In your pom.xml, your plugin.yml should be listed in the <resources> section within your <build> section - here's where you can tell Maven to carry out the replacements, via the <filtering> tag. E.g.:
    Code:
              <resources>
                <resource>
                    <directory>${basedir}/src/main/resources</directory>
                    <filtering>true</filtering>
                    <includes>
                        <include>plugin.yml</include>
                        <include>config.yml</include>
                    </includes>
                </resource>
                <resource>
                    <directory>${basedir}/src/main/resources</directory>
                    <filtering>false</filtering>
                    <includes>
                        <include>denied.png</include>
                    </includes>
                </resource>
            </resources>
    
    Here I've declared that plugin.yml and config.yml will be run through Maven's filtering before inclusion in the final JAR, the the denied.png resource will not be filtered. In plugin.yml, ${project.version} gets replaced with whatever is inside the <version> tag (which is inside the <project> section) in my pom.xml.

    Further reading: https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
     
  30. How would I add my core plugin to the dependencies, so that it will compile?

    EDIT: I fixed it, but now I want to know if there's a way to get maven to auto-export to a given directory?

    EDIT2: Nevermind, I figured it out.
     
Thread Status:
Not open for further replies.

Share This Page