Maven: How to refactor CraftBukkit package paths?

Discussion in 'Plugin Development' started by bergerkiller, May 8, 2013.

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

    bergerkiller

    So I decided to properly add refactoring in the pom.xml of BKCommonLib so I can point to net.minecraft.server, after which that is properly translated to whatever package version applied. How do I go about doing this?

    In source code:
    Compiled:
    It's a simple task to do when shading in the library in your own file (maven-shade-plugin relocation) but I've been unable to do so with non-shaded libraries that are external. Plus, the relocation doesn't show up in BKCommonLib, so I used the path in the library but THAT was relocated to my own path.

    Anyone managed to do this? And note: I am NOT talking about adding a separate sub-module for a specific NMS version and shading all of these versions in it, that is not feasible for the size of BKC.

    pom.xml of BKCommonLib is here.
     
  2. Offline

    bergerkiller

    Final bump
     
  3. Well, I have been trying to do that once they added the policy but I have come to the conclusion that it is not possible without making some sort of own compiler plugin for maven. As you know relocations of the packages is ok, but relocation or renaming of imports seems to be not doable from what things I have tried. I haven't gotten around to try and attempt to create my own compiler plugin in maven which would just rename the imports. Actually I don't know if a plugin would be capable of that but as far as I read, it might actually work.

    Success!
    Well, I didn't expect to get this working, but it actually works :D
    Moreover, you can actually change anything in the file if you want and let it get compiled, so you can even change inline imports and such. I don't know how you solved it but there will be errors in the IDE, but not when when it's getting compiled. You have to change your project a bit, because I need to put the sources to a temporary folder in order to change them.
    It also appears that when I'm using it maven thinks that the goal, it is defined under, is not correct, even though it doesn't spit out and error when running.

    For example you build cycle would now look like this:
    Code:
    <build>
        <sourceDirectory>${basedir}/targetTemp/</sourceDirectory>
        <directory>${basedir}/target</directory>
        <plugins>
          <plugin>
            <groupId>de.kumpelblase2.plugin</groupId>
            <artifactId>relocation-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>relocate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
              <sourceDir>${basedir}/src/main/java</sourceDir>
              <properties>
                <property>
                    <name>org.bukkit.craftbukkit.</name>
                    <value>org.bukkit.craftbukkit.v1_5_R3.</value>
                </property>
              </properties>
            </configuration>
          </plugin>
            ....
            ....
        </plugins>
      </build>
    Input:
    Code:
    package de.kumpelblase2.testing;
     
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.craftbukkit.CraftWorld;
     
    public class Testing extends JavaPlugin {
        CraftWorld world;
        org.bukkit.craftbukkit.CraftServer server;
    }
    Output:
    Code:
    package de.kumpelblase2.testing;
     
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.craftbukkit.v1_5_R3.CraftWorld;
     
    public class Testing extends JavaPlugin {
        CraftWorld world;
        org.bukkit.craftbukkit.v1_5_R3.CraftServer server;
    }
    
    Would that fill your needs?

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

    bergerkiller

    kumpelblase2
    It looks exactly like what I need, only issue is that my maven can't find your maven plugin so I can't test it... :/

    The compiler errors shown in eclipse then is going to cause problems though...because it results in 'class hierarchy errors' here and there, resulting in all other errors being ignored until they are fixed. It makes debugging everything a bit too hard. To fix it, I may decide to build against a CB and Spigot fork that has versioning excluded. (but I fear that will result in compiling failing...can I do this relocation after compiling?)

    EDIT

    What on earth did I just find?!?! :O
     
    kumpelblase2 likes this.
  5. bergerkiller haha, nice find!

    To everyone who wants to use mine still, it is up on my maven repo.
    Code:
        ...
      <repositories>
            <repository>
                <id>relocate-repository</id>
                <url>http://repo.infinityblade.de/relocate-plugin/releases</url>
            </repository>
            ...
      </repositories>
     
      ...
     
      <build>
        <sourceDirectory>${basedir}/targetTemp/</sourceDirectory><!-- This is the temporary directory for the classes. Don't use your normal source directory or those will get overriden. -->
        <directory>${basedir}/target</directory>
        <pluginManagement>
            <plugins>
              <plugin>
                <groupId>de.kumpelblase2.plugin</groupId>
                <artifactId>relocation-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>relocation</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                  <sourceDir>${basedir}/src/java/main</sourceDir><!-- This is your normal source directory -->
                  <properties>
                    <property><!-- Add as many properties as you like. Each property represents a replacement -->
                        <name>org.bukkit.craftbukkit.</name><!-- This is the import it should search for and those should be in the <name> - tag  -->
                        <value>org.bukkit.craftbukkit.v1_5_R3.</value><!-- The import it should get replaced with -->
                    </property>
                  </properties>
                </configuration>
              </plugin>
             
                ...
             
            </plugins>
        </pluginManagement>
      </build>
    </project>
     
  6. Offline

    bergerkiller

    kumpelblase2
    It still could not find the maven plugin of yours after adding the repository, I think maven looks elsewhere to find maven plugins...maybe not under repositories? Same error:
    I also think that the one I linked is no longer being managed, so yeah, I can't use it. No idea where to download the compiled version from, and if I try to compile it myself, I would have no idea where to put the jar it produces...
     
  7. Offline

    Comphenix

    Just compile it with "mvn clean install". That will sttore the compiled JAR in your local Maven repository, which takes precedence over any external repository. Maven should find it then.

    On Windows, this local repository can be found under %UserProfile\.m2 (or C:\Users\%USERNAME%\.m2). Often, it can be useful to delete the sub folder corresponding to the artifact group if Maven refuses to recheck the external repository. That will clear any "timestamps" of the last download attempt, and force Maven to redownload the artifact.
     
    bergerkiller likes this.
  8. Offline

    bergerkiller

    Comphenix
    Appeared to work, but sadly the maven plugin I linked before is broken. Failed with an NPE somewhere...

    If anyone is able to provide some sort of 'virtual dependency package path changer', that would be of great help, not just to me I'm sure. Afraid there is no such plugin yet for maven :(

    I found my solution, but it contains <disallowed words> and posting it results in a direct alert to <person>. I managed to get it to work, you can see it for yourself inthe (public) BKC source code.

    Part of it involves using the shading maven plugin to relocate the packages...which worked to my surprise. I thought it was only possible for including dependencies (shading), but you can also use it to relocate your packages.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page