76

I'm an Intellij noob using v13.

I have a simple java project (default) structure that looks like:

  [] .idea
  [] src
     [] com.test
          mytestClass1
          mytestClass2

Now, I want to write a simple test and I'd like to put that code in /src/test/java directory. So I decided to change my project format so that it looks like:

 [] .idea
   [] src
     [] main
        [] java    
        [] resources
     [] test
        [] java    
        [] resources

After I create that structure, I'd obviously like to move my current classes in src to the main/java/com... structure.

I've read all about content roots and project structure. The info even states, "Folders within content roots can be assigned to the following categories: sources, test sources, generated sources, generated test sources and excluded folders (in Java modules - also resources and test resources)"

I've tried right-clicking on the project name then New -> Directory then entering "src/main/java" (also tried it with a leading slash) but when I right-click my new directory and select "Mark Directory As" the only option is Excluded.

If I create a new directory in the root, I can select Mark Directory As Test Sources Root but it still hangs off the root like:

  [] .idea
  [] java
  [] src
     [] com.test

Can someone please tell me how to migrate to my desired structure?

eze
  • 2,332
  • 3
  • 19
  • 30
  • 1
    The directories structures for tests and tested classes must fit. Here is how to do it easily: stackoverflow.com/a/36057080/715269 – Gangnus Mar 17 '16 at 10:10

1 Answers1

152

You should have structure like this one:

 [] .idea    
 [] src
    [] main
      [] java   
           com.simpleproject
             SimpleClass.java
      [] resources
    [] test
      [] java
           com.simpleproject
             SimpleClassTest.java
      [] resources

After that go to: File->Project Structure->Modules and in "Sources" tab you can choose which folder is "test folder" (usually java in test), which "sources" (usually java in main) etc by clicking "Mark as" options.

mariusz-s
  • 1,756
  • 2
  • 13
  • 14
  • Nice! Worked. And can you tell me the best way to refactor so that the old classes are in the new structure? (I tried ->Refactor->Move but isn't working as I'd hoped. – eze Jan 27 '15 at 01:53
  • 2
    I am not sure I understand but I think "drag and drop" package folder/class in project view on the left side of IDE will help :) – mariusz-s Jan 27 '15 at 16:17
  • I needed to create the package in the new location before dragging any of the classes or sub packages. I right-clicked on my new java folder and doing a create -> package. Then I could drag classes from the old location to the new one without messing up package info. Moving a sub-package worked then also by choosing "Move everything from .....to another directory" on the Select Refactoring window. Thanks. – eze Jan 27 '15 at 17:30
  • 4
    This still seems to be the correct procedure for IntelliJ 14. Why is it so complicated? Is it because IntelliJ normally adds the test sources root on project creation? – Patrick M Jun 02 '15 at 17:42
  • 1
    It needn't be so complicated. Look here: stackoverflow.com/a/36057080/715269 – Gangnus Mar 17 '16 at 10:10
  • This does not work. `java` is a "prohibited package name". – Atte Juvonen Jan 13 '17 at 15:47
  • Works after setting `sources` to `java` instead of `main`. – Atte Juvonen Jan 13 '17 at 16:11
  • 3
    I am always unclear on one detail. In this example is com.simpleproject a package name in the classes or a folder under java (or scala) in the project? – Eric Aldinger Oct 04 '17 at 20:08