mfnalex / ChestSort

Best sorting plugin for Minecraft / Spigot 1.14+
https://www.chestsort.de
GNU General Public License v3.0
91 stars 34 forks source link

Convert the project to use Maven #7

Closed Azzurite closed 5 years ago

Azzurite commented 5 years ago

I did this for myself, I just wanted to suggest maybe adding it to your main repository if you want.

Nothing changed, except that I moved the .java files to src/main/java and the resources (config.yml, plugin.yml and the default categories) to the src/main/resources folder and that I added a pom.xml with the current version being 3.4, as mentioned in plugin.yml.

You can of course make any adjustments you want, for example in name or groupId.

I mainly did this because I wanted to exclude certain chests from sorting, even if you as a person have /chestsort on. Since I don't use eclipse, I thought I'd convert it to Maven so I could easily build that.

If you want, you can look at the ignored_chests branch in my fork, however, that code is not ready for general public usage. The main thing is that ignored chests have to be somehow cleared when they are removed. And no message keys were used.

mfnalex commented 5 years ago

I have never used maven nor do I actually know what it is. What are the advantages?

Azzurite commented 5 years ago

Oh! I'm very sorry, Maven is just so well known, that I wouldn't have thought there are still people that don't know it! Sorry again. It is a build tool, used to manage dependencies and make builds replicable cross-platform and cross-IDE.

Eclipse (and almost every IDE) has native support for Maven projects.

Basically, the advantage to me using it here was as follows.

You have a .classpath file checked in that define which dependencies to include into ChestSort, Let's go through that one-by-one:

1)

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
    <attributes>
        <attribute name="module" value="true"/>
    </attributes>
</classpathentry>

This basically tells eclipse to simply include everything in the project directory into the final .jar

2)

<classpathentry kind="src" path="src"/>

this tells eclipse to find every .java file under src/ and compile it.

3)

<classpathentry kind="lib" path="P:/BuildTools/Spigot/Spigot-API/target/spigot-api-1.13.2-R0.1-SNAPSHOT-shaded.jar"/>

This tells eclipse to take your local file at P:/BuildTools/Spigot/Spigot-API/target/spigot-api-1.13.2-R0.1-SNAPSHOT-shaded.jar and add it as a dependency to the project.

Now, I had to do the following things/had the following problems:

  1. I don't even really know how to add everything in the project directory to the final .jar in my IDE (and I think it's a bit of a problematic thing, what if you have some temporary file there with some megabytes of data?)
  2. I have to configure src/ as a sources root in my IDE, just a few clicks.
  3. Oh shit? I don't have access to your computer and your local files, where do I get this dependency from? The solution is to go search for it on the web, download it to my computer, and then add it as a library in my IDE, as you did.

Now, let's say I'm someone coming new to this project and the project already uses Maven. What do I have to do in this case? I need to have Maven installed and run mvn install, and I'm done with everything that I previously had to do manually above, I've got a finished .jar.

So basically, by using Maven, you change each person having to set up the project manually to everything happening automatically.

So how Maven works is, you got a pom.xml that basically describes your project. There is some metadata at the top, like how the project is called.

In <properties> these are just some variables usable in the pom.xml.

<build> says how to build the project, everything is achieved through plugins. You only need to add those default maven ones here, if you want to do additional configuration of them, in this case I told the compiler that I want Java 1.8 output and the jar-packager that I want "ChestSort.jar" as the jar name (normally the version is added at the end, I thought I'd keep it like you did name the jar before)

<repositories> tells Maven where to get dependencies like the spigot minecraft API .jar from. This normally doesn't need to be added, as most dependencies are published to "Maven Central" but Spigot doesn't do this, I don't know why, so I had to add their repository here.

<dependencies> actually defines the dependencies, in this case the spigot api. <scope>provided<..> is added, because normally the .jar packaging assumes that you have to add the dependencies to the final jar for the project to work. However, in the case of plugin development for spigot, this API is "provided" by the spigot server, so we don't need to add it.

Then, maven has a default directory structure of src/main/java for .java files and src/main/resources for all files that should go into the jar and onto the classpath, but is not actual sourcecode. I moved all relevant files to those two directories. You could still use the previous project structure, however, you would've needed some more configuration in the pom.xml and this standard directory structure is well known by everyone that uses maven so they will immediately find relevant files.

So, to summarize, using maven has the advantage of builds of the project working all the time, whereever you are, with just one single command and no time needed to set the project up. To enable this, you need to add the pom.xml, which I did here.

Just to show you how widely used Maven is, just go to the explore page on Github: https://github.com/topics/java and see how many project have a "pom.xml" in their root directory. It's the de-facto build tool used when developing in Java. There are some other popular ones, like Gradle, which is arguably better, but still less widely used. I know Maven though, so I added maven. Both are not exclusive, so you could later also add a build.gradle (Gradles equivalent to the pom.xml) if you wanted to.

I hope I could clear that up! If you've got any more questions, ask away.

Azzurite commented 5 years ago

Theoretically, after adding maven, one could also remove all the IDE-specific files, like .project, .classpath and the .settings/ directory and just add them to the .gitignore list. Eclipse auto-generates these from the pom.xml.

I'm not too familiar with Eclipse anymore, since I've been using IntelliJ for years now and never touched Eclipse after, so I can't perfectly tell you how to set a Maven project up, but I'm pretty sure it's as simple as "Import" -> "Maven project" -> select the pom.xml, or something like that.

mfnalex commented 5 years ago

Nice, that is indeed very useful! Thanks for the suggestion

mfnalex commented 5 years ago

I have reverted this. The main reason is I don't see ANY advantage in using maven. The Spigot-API .jar still has to be put in the build path manually (or did I misunderstand the <scope>provided</scope>)? That means, each time a new version is out, I not only have to compile the new API and put it in the build path, but I ALSO have to adjust the xml file. For what? I added the 1.14 API and removed the 1.13 one. Eclipse did not bother, although the xml says I provide a 1.13 file. Is the xml just a description? Why doesn't it produce a warning? Additionally, the source code was broken. All class files have been moved from de.jeffclan.JeffChestSort to something like main.java.de.jeffclan.JeffChestSort. I don't see a reason for this, but it's not a problem. BUT, the class files all declared themselves as de.jeffclan.JeffChestSort, so those all would have to be updated separately.

I used to develop ChestSort on 3 different computers with 3 different operating systems (Mac, Linux Mint, Windows). It worked flawlessly by just importing the project in eclipse via git and then changing the BuildPath. This took like 20 seconds.

Now, when opening the projects, all class files are broken.

I don't think maven saves time - the opposite was the case, at least for me.

Thanks anyway for the suggestion :)

Azzurite commented 5 years ago

@mfnalex , you have to completely delete your existing eclipse project files, then choose "Import... -> Maven Projects" and select the pom.xml

I suspect that you just tried to keep your existing project files, which obviously would still be configured for not-maven.

The package structure gets correctly adjusted (by adding main as a source directory, and not src) when you do this.

And yes, the Spigot API .jar gets automatically downloaded by Maven and put as a dependency into your eclipse. You don't have to mess with any settings inside eclipse anymore, no build path adjustments or anything. You just need to update the version in pom.xml to use 1.14 instead of 1.13 and eclipse does the rest for you.

<scope>provided</scope> just says that the Spigot .jar does not get added to the final built ChestSort.jar output when you run mvn install. It has nothing to do with development, only the final build of the .jar that you distribute.

It worked flawlessly by just importing the project in eclipse via git and then changing the BuildPath

Now you have to "Import -> Maven projects" instead of whatever you did before, but you don't have to adjust the build path anymore, so the 20 seconds go away.

But I can understand why you wouldn't want to use it, in the end, it was not meant for you (since you were already comfortable and familiar with your workflow, the only thing it would do for you is use some new technology which you'd have to learn and deal with) but for others, as most of the Java world works with some kind of build tool. For me, for example, not having a Maven/Gradle/whatever script that does all my IDE configuring for me is normally a huge hurdle in contributing to open-source projects. It was not easy to figure out how to get your project to build, since I've only used eclipse twice in my life.

I wanted to spare anyone else that maybe wanted to contribute to your project the hurdle, after I already did the work of converting for myself anyway. But for you, obviously, you'd have to learn something that you're not used to.

mfnalex commented 5 years ago

Thanks for the further explanation. I am always willing to expand my knowledge :) However, I wanted ChestSort to work with 1.14 as soon as possible, so I decided to remove maven for now.

Once I understand it better, I will convert it again so that contribution gets easier for helpful people like you :)

Thanks again for the suggestion, I will have a closer look at maven in a few days :)

mfnalex commented 5 years ago

I got one question right now: I have to use mvn install to create the jar I distribute? Is that correct? Do I have to do this everytime I want to compile or can I still use Eclipse's Build function?

Greetings

Azzurite commented 5 years ago

mvn install is just to build the finished .jar, eclipse should handle all building during development.

mfnalex commented 5 years ago

Thanks. I will let you know once I know maven well enough to use it properly

mfnalex commented 5 years ago

The class package names are still wrong: https://youtu.be/eRSi-uEdmg8

Any idea how to fix this?

mfnalex commented 5 years ago

All classes produce tons of errors: https://youtu.be/shn8KcnasI8 Nothing has been adjusted by maven. What am I doing wrong? :/

Azzurite commented 5 years ago

You have to run the command mvn install, which produces the final .jar.

Some quick googling gives me this: https://stackoverflow.com/questions/19793895/run-mvn-clean-install-in-eclipse#19794346

The final jar is in the target/ directory.

Azzurite commented 5 years ago

Basically Maven replaces everything that is normally IDE-specific, so you have to use Maven to do what you want. Export JAR is a feature that is eclipse-specific and needs some kind of setup there, I don't know.

Azzurite commented 5 years ago

Theoretically you would also have to do mvn compile to even compile the project, but eclipse does that automatically with the Maven plugin. Apparently the maven integration plugin in eclipse is not perfect and does not change the "Export JAR" feature that you have been using so far.

mfnalex commented 5 years ago

As you can see in the second video, it is not possible to properly work with the source code using maven because eclipse does not recognize imports from org.bukkit

Azzurite commented 5 years ago

Try deleting the existing eclipse project files before importing the project...

Azzurite commented 5 years ago

I'm downloading eclipse and trying it out myself.

mfnalex commented 5 years ago

I installed eclipse on my MacBook from scratch and imported the project into an empty workspace. Still the same: https://youtu.be/O3FHDu5Kh0Y

(Video is still in processing by youtube, might take 5 minutes before you can see it)

Azzurite commented 5 years ago

I have the same problems as you when I just import the project from the commit. As soon as I delete the .settings/ directory and the .project and .settings file, everything works.

mfnalex commented 5 years ago

Thanks, I will try it right now

Azzurite commented 5 years ago

I installed eclipse on my MacBook from scratch and imported the project into an empty workspace. Still the same

Yes but did you delete the eclipse project files I mentioned before?

mfnalex commented 5 years ago

I will try this now :) Thanks for the help

Azzurite commented 5 years ago

Apparently eclipse, even when you explicitly import a maven project, uses its own project files first instead of the maven ones...

mfnalex commented 5 years ago

That's kind of stupid behaviour by eclipse. Maybe it would have worked from the beginning if I would have created the maven version within eclipse.

Azzurite commented 5 years ago

Or if I had deleted the existing project files in my commit in this PR :D I just thought it wouldn't matter

Azzurite commented 5 years ago

You can then create a run configuration like so: image

so you can use that to build the project and create the final .jar

mfnalex commented 5 years ago

Yeah sure, but I think it's stupid none the less: If eclipse sees that it's a maven project, it should ignore all the eclipse config stuff that has been just a leftover. I don't know :D I will convert it to maven myself now and hope that it will be useful :)

Thanks for your help 👍

mfnalex commented 5 years ago

Before I mess something up with the maven conversion: Would you be so kind and convert the latest source (again) to maven (+ remove the leftover eclipse files) and submit a PR? :)

Azzurite commented 5 years ago

I can do that, but have to do it a little later.

mfnalex commented 5 years ago

No problem, take your time. It's not urgent :)

mfnalex commented 2 years ago

THANK YOU VERY MUCH for introducing me to maven! I was such a smallbrain 2 years ago. Meanwhile I couldn't live without maven anymore. Also a big THANK YOU for the excellent detailled explanation you provided! :)

noramibuu commented 2 years ago

THANK YOU VERY MUCH for introducing me to maven! I was such a smallbrain 2 years ago. Meanwhile I couldn't live without maven anymore. Also a big THANK YOU for the excellent detailled explanation you provided! :)

I was like "oh, alex (who teached maven to me) trolled someone 2 years ago about maven" lmao

mfnalex commented 2 years ago

I didn't, Mr. Cartman basically forced me to check out maven and I am very glad that he did :D

Azzurite commented 2 years ago

haha glad I could help :) if you need anything else, let me know :)