Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/IntelliJ keep classes in another folder but in the same package

Is there a way to keep your classes separated in different folders without keeping them in a different package?

I have a collection of about 40 classes, and I do want all of them to be in the same package, but I want a way to visually group them so it's a bit easier to navigate as this grows in size.

Is there a way to do this with IntelliJ? In Java in general?

The reason I don't want sub-packages is as follows:

Foo in package Foo:

package Foo;
public class Foo {
    public static void main(String[] args) {
        Bar.FooBar(); //<--compile error
    }
}

and Bar in package Foo.Bar:

package Foo.Bar;

public class Bar {
    static void FooBar() {}
}
like image 699
Joseph Nields Avatar asked Nov 16 '25 23:11

Joseph Nields


2 Answers

If you have more than one source classpath with the same package structure, you can do this.

In IntelliJ, you can simply make a new folder and then mark that folder as a Source folder. (Right Click folder -> Mark Directory As -> Sources Root). Again, as long as the package structure is the same, all classes in the same packages will end up together.

like image 155
Hypino Avatar answered Nov 18 '25 12:11

Hypino


If you use maven in IntelliJ, you could use the build-helper-maven-plugin in your build section of your pom file like this:

<build>
   <plugins>
       <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>build-helper-maven-plugin</artifactId>
         <version>1.9.1</version>
         <executions>
            <execution>
               <id>src</id>
               <phase>generate-sources</phase>
               <goals>
                  <goal>add-source</goal>
               </goals>
               <configuration>
                  <sources>
                     <source>../your/additional/source/path</source>
                  </sources>
               </configuration>
            </execution>
         </executions>
      </plugin>
   </plugins>
</build>

This way you can separate your code into multiple logical folders while using the same package in the project, and maven will take care of grouping them all together when you run any maven command.

like image 45
Joe Almore Avatar answered Nov 18 '25 13:11

Joe Almore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!