Mikhail Vega
Mikhail Vega

Reputation: 511

Maven: No sources to compile

I am following the 'Build Java Projects with Maven' (https://spring.io/guides/gs/maven/#scratch) and when I run 'mvn compile' from /Users/Misha/Desktop/src/main/java/hello, I get this prompt:

[INFO] Scanning for projects...   
[INFO]                                                                                  
[INFO] ------------------------------------------------------------------------   
[INFO] Building gs-maven 0.1.0    
[INFO] ------------------------------------------------------------------------   
[INFO]    
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gs-maven ---   
[INFO] Using 'UTF-8' encoding to copy filtered resources.   
[INFO] Copying 3 resources   
[INFO]    
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ gs-maven ---   
[INFO] No sources to compile   
[INFO] ------------------------------------------------------------------------   
[INFO] BUILD SUCCESS     
[INFO] ------------------------------------------------------------------------    
[INFO] Total time: 0.942 s    
[INFO] Finished at: 2015-01-11T23:10:28-08:00   
[INFO] Final Memory: 7M/155M
[INFO] ------------------------------------------------------------------------

I have the two java files and the xml file in the hello directory, and I am assuming that I should see "Hello World!" instead of No sources to compile. Why is my java code not compiling? Thanks!

Upvotes: 50

Views: 68870

Answers (8)

cmlonder
cmlonder

Reputation: 2520

I was missing sourceDirectory section in my build, which was causing no-arg plugin not to work.

If you run kotlin:compile, output shows "No sources to compile". Sharing my complete build section if it helps someone struggling make it work as 2024.

<build>
        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot-maven-plugin.version}</version>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>all-open</plugin>
                        <plugin>spring</plugin>
                        <plugin>no-arg</plugin>
                        <plugin>jpa</plugin>
                    </compilerPlugins>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>

                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-noarg</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

Upvotes: 1

Erik Finnman
Erik Finnman

Reputation: 1667

I was adding some Java code to a multi-module Kotlin project and in the top level pom.xml the following was defined:

<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>

So the Java code was never detected by Maven until I added the Java directories to the build configuration:

    <build>
        <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
...

Upvotes: 1

jacnes
jacnes

Reputation: 1

In my case, I had to downgrade JDK version from JDK 18 to JDK 17 (IDE: Apache NetBeans 15)

Upvotes: 0

Ranjith_chary
Ranjith_chary

Reputation: 1

The recommended git tool is: NONE using credential fcdbaabd-0a76-4111-876f-f6c6a5dfb89a

/usr/bin/git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/test3/.git # timeout=10 Fetching changes from the remote Git repository /usr/bin/git config remote.origin.url https://github.com/Ranjithchary/realme-stand.git # timeout=10 Fetching upstream changes from https://github.com/Ranjithchary/realme-stand.git /usr/bin/git --version # timeout=10 git --version # 'git version 2.37.1' using GIT_ASKPASS to set credentials /usr/bin/git fetch --tags --force --progress -- https://github.com/Ranjithchary/realme-stand.git +refs/heads/:refs/remotes/origin/ # timeout=10 /usr/bin/git rev-parse refs/remotes/origin/main^{commit} # timeout=10 Checking out Revision a37696f887a202dc75b5e7860709b5fff3467998 (refs/remotes/origin/main) /usr/bin/git config core.sparsecheckout # timeout=10 /usr/bin/git checkout -f a37696f887a202dc75b5e7860709b5fff3467998 # timeout=10 Commit message: "files added" /usr/bin/git rev-list --no-walk a37696f887a202dc75b5e7860709b5fff3467998 # timeout=10 [test3] $ mvn mvn install Error: JAVA_HOME is not defined correctly. We cannot execute /opt/openjdk-11.0.2_linux-x64/bin/java Build step 'Invoke top-level Maven targets' marked build as failure Finished: FAILURE

Upvotes: -1

Cameron Hudson
Cameron Hudson

Reputation: 3931

In my case, I was missing this:

<project>
    ...
    <build>
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>test</testSourceDirectory>
    </build>
    ...
</project>

Normally, I'd just use the default directory structure

  • src/main/java as a source folder.
  • src/test/java as a test folder.

But I'm working on a class project with existing code, and can't rearrange the file structure.

Upvotes: 29

P.Brian
P.Brian

Reputation: 46

Are you try to compile project or class ? As the guideline in https://spring.io/guides/gs/maven/#scratch you need to compile for project. Try to run mvn compile from project direction.

Upvotes: 3

user2879704
user2879704

Reputation:

because there are no java files in $PROJECT_DIR/src/main/java

Upvotes: 4

piet.t
piet.t

Reputation: 11911

To create a maven-project you need

  1. A project-directory containing the pom.xml-file
  2. Within this project-directory a subdirectory src/main/java containing your java-code (packages go to subdirectories of src/main/java)

To invoke maven run mvn compile or something similar from the project-directory.

Upvotes: 66

Related Questions