Alessandro Bertulli
Alessandro Bertulli

Reputation: 500

Why doesn't Maven recognize file modifications? ("Nothing to compile - all classes are up to date" error)

I have a project in Java + Maven. I can compile and run the code with IntelliJ just fine: each modification I do in the code gets compiled in the program. However, when I try compiling from the command line (using mvn compile), Maven doesn't recognize the changes in the code, and simply says "Nothing to compile - all classes are up to date". I can force rebuilding using mvn clean, but that invalidates the purpose of having a build system. Why is it so, and how can it detect changes in the code?

For example, say I have a working code.

  1. I do mvn compile. It works fine and I can run the program.
  2. I change the code and introduce a syntax error. I save the file and do mvn compile. The output is Nothing to compile, together with a BUILD SUCCESS. However, when I try to run the program, I get a Exception in thread "main" java.lang.Error: Unresolved compilation problems, along with a series of error (due to the erroneous parsing)
  3. If I change the code to a different code, but syntactically valid, I get the same behavior.
  4. In fact, after this, even returning to my original code compiles, but then throws a java.lang.Error exception!

Why does it work like this, and why does it work in IntelliJ, but not in terminal?


EDIT: IntelliJ is not open. However, Emacs does start the language server (jdtls).

My pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>it.polimi.middlewareB</groupId>
  <artifactId>akka_actors</artifactId>
  <packaging>jar</packaging>
  <version>0.1  </version>

  <properties>
    <akka.version>2.6.19</akka.version>
    <scala.binary.version>2.13</scala.binary.version>
  </properties>
  
  <name>akka_actors</name>
  <url>http://maven.apache.org</url>

  <dependencyManagement>
    <dependencies>
      <dependency>
    <groupId>com.typesafe.akka</groupId>
    <artifactId>akka-bom_${scala.binary.version}</artifactId>
    <version>2.6.19</version>
    <type>pom</type>
    <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  
  <dependencies>
    <dependency>
      <groupId>com.typesafe.akka</groupId>
      <artifactId>akka-actor-typed_2.13</artifactId>
      <version>${akka.version}</version>
    </dependency>
    <dependency>
      <groupId>com.typesafe.akka</groupId>
      <artifactId>akka-actor_${scala.binary.version}</artifactId>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
    </dependency>
    <dependency>
      <groupId>com.typesafe.akka</groupId>
      <artifactId>akka-actor-testkit-typed_2.13</artifactId>
      <version>${akka.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.kafka</groupId>
      <artifactId>kafka-clients</artifactId>
      <version>3.2.0</version>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.17.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.17.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
      <version>2.17.2</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.12.6.1</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
          <source>11</source>
          <target>11</target>
    </configuration>
      </plugin>
      <!-- <plugin> -->
      <!--   <groupId>org.codehaus.mojo</groupId> -->
      <!--   <artifactId>exec-maven-plugin</artifactId> -->
      <!--   <version>1.6.0</version> -->
      <!--   <configuration> -->
      <!--     <executable>java</executable> -->
      <!--     <arguments> -->
      <!--       <argument>-classpath</argument> -->
      <!--       <classpath /> -->
      <!--       <argument>com.example.AkkaQuickstart</argument> -->
      <!--     </arguments> -->
      <!--   </configuration> -->
      <!-- </plugin> -->
      <!-- <plugin> -->
      <!--   <groupId>org.codehaus.mojo</groupId> -->
      <!--   <artifactId>exec-maven-plugin</artifactId> -->
      <!--   <version>1.6.0</version> -->
      <!--   <executions> -->
      <!--     <execution> -->
      <!--       <id>default-cli</id> -->
      <!--       <configuration> -->
      <!--         <mainClass>com.example.AkkaQuickstart</mainClass> -->
      <!--       </configuration> -->
      <!--     </execution> -->
      <!--   </executions> -->
      <!-- </plugin> -->
    </plugins>
  </build>
</project>

After mvn clean, this is the output of mvn compile:

[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< it.polimi.middlewareB:akka_actors >------------------
[INFO] Building akka_actors 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ akka_actors ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ akka_actors ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 15 source files to /home/alessandro/middleware/middleware_projectB/akka_actors/target/classes
[INFO] /home/alessandro/middleware/middleware_projectB/akka_actors/src/main/java/it/polimi/middlewareB/AlwaysSeekToBeginningListener.java: /home/alessandro/middleware/middleware_projectB/akka_actors/src/main/java/it/polimi/middlewareB/AlwaysSeekToBeginningListener.java uses unchecked or unsafe operations.
[INFO] /home/alessandro/middleware/middleware_projectB/akka_actors/src/main/java/it/polimi/middlewareB/AlwaysSeekToBeginningListener.java: Recompile with -Xlint:unchecked for details.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.471 s
[INFO] Finished at: 2022-07-13T10:36:08+02:00
[INFO] ------------------------------------------------------------------------

Adding in the main .java file a line like this

public class ClusterStarter {
    private static final String kafkaDefaultArgument = "localhost:9092";
    private static final int actorPoolDefaultDimension = 3;
    apinaosncla aifasi; //NOTE HERE
    public static void main(String[] args) {
/* ... */
    }
}

launching mvn compile gives:

[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< it.polimi.middlewareB:akka_actors >------------------
[INFO] Building akka_actors 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ akka_actors ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ akka_actors ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.970 s
[INFO] Finished at: 2022-07-13T10:43:07+02:00
[INFO] ------------------------------------------------------------------------

However, giving first mvn clean and then mvn compile:

[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< it.polimi.middlewareB:akka_actors >------------------
[INFO] Building akka_actors 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ akka_actors ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ akka_actors ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 15 source files to /home/alessandro/middleware/middleware_projectB/akka_actors/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/alessandro/middleware/middleware_projectB/akka_actors/src/main/java/it/polimi/middlewareB/ClusterStarter.java:[36,5] cannot find symbol
  symbol:   class apinaosncla
  location: class it.polimi.middlewareB.ClusterStarter
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.966 s
[INFO] Finished at: 2022-07-13T10:43:46+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project akka_actors: Compilation failure
[ERROR] /home/alessandro/middleware/middleware_projectB/akka_actors/src/main/java/it/polimi/middlewareB/ClusterStarter.java:[36,5] cannot find symbol
[ERROR]   symbol:   class apinaosncla
[ERROR]   location: class it.polimi.middlewareB.ClusterStarter
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Now, returning to a working code, mvn compile still gives Nothing to compile, and launching the program gives:

/path/to/project $ /usr/lib/jvm/java-11-openjdk/bin/java -Dfile.encoding=UTF-8 -classpath /home/alessandro/middleware/middleware_projectB/akka_actors/target/classes:/home/alessandro/.m2/repository/com/typesafe/akka/akka-actor-typed_2.13/2.6.19/akka-actor-typed_2.13-2.6.19.jar:/home/alessandro/.m2/repository/org/scala-lang/scala-library/2.13.8/scala-library-2.13.8.jar:/home/alessandro/.m2/repository/com/typesafe/akka/akka-slf4j_2.13/2.6.19/akka-slf4j_2.13-2.6.19.jar:/home/alessandro/.m2/repository/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar:/home/alessandro/.m2/repository/com/typesafe/akka/akka-actor_2.13/2.6.19/akka-actor_2.13-2.6.19.jar:/home/alessandro/.m2/repository/com/typesafe/config/1.4.2/config-1.4.2.jar:/home/alessandro/.m2/repository/org/scala-lang/modules/scala-java8-compat_2.13/1.0.0/scala-java8-compat_2.13-1.0.0.jar:/home/alessandro/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/home/alessandro/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/home/alessandro/.m2/repository/com/typesafe/akka/akka-actor-testkit-typed_2.13/2.6.19/akka-actor-testkit-typed_2.13-2.6.19.jar:/home/alessandro/.m2/repository/com/typesafe/akka/akka-testkit_2.13/2.6.19/akka-testkit_2.13-2.6.19.jar:/home/alessandro/.m2/repository/org/apache/kafka/kafka-clients/3.2.0/kafka-clients-3.2.0.jar:/home/alessandro/.m2/repository/com/github/luben/zstd-jni/1.5.2-1/zstd-jni-1.5.2-1.jar:/home/alessandro/.m2/repository/org/lz4/lz4-java/1.8.0/lz4-java-1.8.0.jar:/home/alessandro/.m2/repository/org/xerial/snappy/snappy-java/1.1.8.4/snappy-java-1.1.8.4.jar:/home/alessandro/.m2/repository/org/apache/logging/log4j/log4j-api/2.17.2/log4j-api-2.17.2.jar:/home/alessandro/.m2/repository/org/apache/logging/log4j/log4j-core/2.17.2/log4j-core-2.17.2.jar:/home/alessandro/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.17.2/log4j-slf4j-impl-2.17.2.jar:/home/alessandro/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar:/home/alessandro/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.12.6.1/jackson-databind-2.12.6.1.jar:/home/alessandro/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.12.6/jackson-annotations-2.12.6.jar:/home/alessandro/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.12.6/jackson-core-2.12.6.jar it.polimi.middlewareB.ClusterStarter
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    RetriesAnalysisActor cannot be resolved
    JobSupervisorActor cannot be resolved
    CompletedAnalysisThread cannot be resolved to a type
    PendingAnalysisThread cannot be resolved to a type

    at it.polimi.middlewareB.ClusterStarter.main(ClusterStarter.java:55)

(those are classes of my project)

Upvotes: 0

Views: 1426

Answers (1)

aakash dutta
aakash dutta

Reputation: 1

what IDE are you using? I had same problem as yours also when i tried to compile with any syntax error my compiler didnt stop building the project, it displayed build successful and i was using STS IDE. what solved my problem was I had to turn off my build automatically option from project tab of the IDE. And after that whenever i tried to compile using command line, the maven compiler detected the changed files and failed if any java related error was detected

Upvotes: 0

Related Questions