48

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!

5
  • 1
    you need to run it from the same directory where pom.xml is placed
    – jmj
    Jan 12, 2015 at 7:24
  • everything is placed in the ./hello directory -- the two java files and the pom.xml file Jan 12, 2015 at 7:26
  • 3
    The java files shoule be placed in ./hello/src/main/java/your/packages/of/java/files. Jan 12, 2015 at 7:27
  • 4
    Your pom should be in the root of the project, i.e. where the src directory is not in src/main/java/hello.
    – M. Deinum
    Jan 12, 2015 at 7:28
  • thank you everyone, i realized my mistake Jan 12, 2015 at 7:41

7 Answers 7

64

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.

24

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.

0
4

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

3

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.

1

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>
...
0

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

-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

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.