pojobuilder icon indicating copy to clipboard operation
pojobuilder copied to clipboard

Intellij IDEA support

Open artemik opened this issue 7 years ago • 6 comments

Is there going to be Intellij IDEA support?

artemik avatar Feb 23 '18 18:02 artemik

I've just found out support is out there. I needed to compile project and mark the generated folder as generated sources root. But the documentation does't seem to indicate that. Could you please add it?

artemik avatar Feb 23 '18 20:02 artemik

Hi Artemik,

Since I haven't used it so far, I didn't include any docs about how to enable PB in IntelliJ IDEA. I'm glad that you managed it anyway.

My question is now, would you be so kind to provide some lines that I can add to the PB documentation, so that a typical IntelliJ user could follow it easy?

mkarneim avatar Mar 15 '18 07:03 mkarneim

It's not perfect, but I was able to get my build / intellij working with the following added to my Gradle script:

apply plugin: 'idea'

ext {
  generatedSourcesRoot = "${projectDir}/src/generated-sources/java"
}

compileJava.options.compilerArgs += ['-s', generatedSourcesRoot]

idea.module {
  generatedSourceDirs += file(generatedSourcesRoot)
  sourceDirs += file(generatedSourcesRoot)
}

PickySalamander avatar Jun 25 '18 19:06 PickySalamander

Thank you for this hint. However, it seems that this is not enough for creating some valid idea project files.

Based on your code snippet I created the following sample project:

build.gradle

apply plugin: 'idea'
apply plugin: 'java'

ext {
  generatedSourcesRoot = "${projectDir}/src/generated-sources/java"
}

compileJava.options.compilerArgs += ['-s', generatedSourcesRoot]

idea {
  project {
    jdkName = '1.8'
    languageLevel = '1.8'
  }
  module {
    generatedSourceDirs += file(generatedSourcesRoot)
    sourceDirs += file(generatedSourcesRoot)
  }
}

repositories {
  mavenCentral()
}

dependencies {
  compile     'net.karneim:pojobuilder:4.2.2'
  testCompile 'org.assertj:assertj-core:2.8.0'
  testCompile 'junit:junit:4.12'
}

Then I added the following two classes:

src/main/java/sample/Contact.java

package sample;

import net.karneim.pojobuilder.GeneratePojoBuilder;

@GeneratePojoBuilder
public class Contact {
  public String name;
  public int age;
}

src/test/java/sample/ContactTest.java

package sample;
import org.junit.Test;
import org.assertj.core.api.Assertions;

public class ContactTest extends Assertions {
  @Test
  public void test() {
    ContactBuilder underTest = new ContactBuilder();
    Contact act = underTest.withName("blah").build();
    assertThat(act.name).isEqualTo("blah");
  }
}

Finally I executed gradlew idea from the command line and then opened the project folder with Idea.

When I now invoke "Build > Build Project" I just get the following compile error:

C:\devel\workspace\pojobuilder-idea-sample\src\test\java\sample\ContactTest.java
Error:(8, 5) java: cannot find symbol
  symbol:   class ContactBuilder
  location: class sample.ContactTest
Error:(8, 36) java: cannot find symbol
  symbol:   class ContactBuilder
  location: class sample.ContactTest

So I guess something is missing :-/

(By the way: building this project from the command line using Gradle works fine.)

mkarneim avatar Jun 26 '18 07:06 mkarneim

If its still a thing, I put my everyday configs for that in a little example project ;) https://github.com/Poeschl/Pojobuilder-Sourceset-Example

I use a dedicated sourceset for the generated classes, so it works out of the box for Intellij and eclipse.

Poeschl avatar Nov 07 '18 07:11 Poeschl

This was a support question and the author never stated they are use Gradle. Should be closed.

  • With Maven, you need to import, compile, re-import to get the generated-sources/annotations folder added to the IDE. This is a pretty standard dance for all annotation-processors/code-generators in IntelliJ
  • With Gradle, the wiki has several cookbook examples that should cover this.

drekbour avatar Mar 04 '19 11:03 drekbour