Search code examples
javaintellij-ideadesign-by-contract

Using cofoja annotations in IntelliJ


I'm fairly new to IntelliJ. When attempting to use cofoja code annotations I receive the following errors when building.

Error:java: error in contract: package com.google.java.contract.core.agent does not exist
Error:java: error in contract: package com.google.java.contract does not exist

My project is targeting Oracle JDK 1.7. I'm using the version of cofoja that io.konverge released and IntelliJ 14.1.4.

<dependency>
  <groupId>io.konverge</groupId>
  <artifactId>cofoja</artifactId>
  <version>2.0.0</version>
</dependency>

I do not receive encounter errors when building the project from the command line with maven.

EDIT:

Here's a sample cofoja project that compiles fine with maven, but that I cannot build in IntelliJ.

https://github.com/konvergeio/cofoja-example

I've attached the IntelliJ DEBUG build log for this project in a cofoja project ticket on GitHub: https://github.com/nhatminhle/cofoja/issues/45

EDIT 2:

Updated question to be specific to the cofoja-example project.


Solution

  • I was able to successfully configure IntelliJ. Hopefully someone finds this useful.

    I had to configure Annotation Processors for the current project. It was enabled by default, but without the appropriate configuration required for classoutput, classpath, and sourcepath settings. I discovered the $PROJECT_DIR$ macro to make relative paths to appropriate project directories. IntelliJ had a pre-configured macro/variable to my local maven repository, so I used that to refer to konvergeio's published version of cofoja.

    I took a screenshot of my configuration:

    intellij-cofoja-annotation-processor

    IntelliJ's resulting build log:

    2015-07-12 14:04:11,448 [   1223]  DEBUG - s.incremental.java.JavaBuilder - Compiling chunk [cofoja-example] with options: "-g -deprecation -encoding UTF-8 -source 1.7 -target 1.7 -Acom.google.java.contract.sourcepath=/home/seglo/source/cofoja-example/src/main/java -Acom.google.java.contract.classpath=/home/seglo/.m2/repository/io/konverge/cofoja/2.0.0/cofoja-2.0.0.jar -Acom.google.java.contract.classoutput=/home/seglo/source/cofoja-example/target/classes -s /home/seglo/source/cofoja-example/target/generated-sources/annotations" 
    

    I pushed the fork of Nas's cofoja-example project to my github. I included the IntelliJ project settings.

    https://github.com/seglo/cofoja-example

    My project's .idea/compiler.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project version="4">
      <component name="CompilerConfiguration">
        <option name="DEFAULT_COMPILER" value="Javac" />
        <resourceExtensions />
        <wildcardResourcePatterns>
          <entry name="!?*.java" />
          <entry name="!?*.form" />
          <entry name="!?*.class" />
          <entry name="!?*.groovy" />
          <entry name="!?*.scala" />
          <entry name="!?*.flex" />
          <entry name="!?*.kt" />
          <entry name="!?*.clj" />
          <entry name="!?*.aj" />
        </wildcardResourcePatterns>
        <annotationProcessing>
          <profile default="true" name="Default" enabled="false">
            <processorPath useClasspath="true" />
          </profile>
          <profile default="false" name="Maven default annotation processors profile" enabled="true">
            <sourceOutputDir name="target/generated-sources/annotations" />
            <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
            <outputRelativeToContentRoot value="true" />
            <option name="com.google.java.contract.classoutput" value="$PROJECT_DIR$/target/classes" />
            <option name="com.google.java.contract.classpath" value="$MAVEN_REPOSITORY$/io/konverge/cofoja/2.0.0/cofoja-2.0.0.jar" />
            <option name="com.google.java.contract.sourcepath" value="$PROJECT_DIR$/src/main/java" />
            <processorPath useClasspath="true" />
            <module name="cofoja-example" />
          </profile>
        </annotationProcessing>
        <bytecodeTargetLevel>
          <module name="cofoja-example" target="1.7" />
        </bytecodeTargetLevel>
      </component>
    </project>