12

I am using a custom directory structure and have specified the directory in sourcedirectory tag. But still I get the message No sources to compile. Although the build is successful.

My directory structure:

enter image description here

So instead of src/main/java, I am using java. (And I have a reason to do that, so right now it's not possible to switch to src/main/java)

Here's my pom.xml:

<artifactId>application</artifactId>  
  <name>application</name>
  <packaging>jar</packaging>

   <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.test.skip>true</maven.test.skip>
  </properties>
    <build>
        <sourceDirectory>java</sourceDirectory>    
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <excludes>
                    <exclude>**/old/**/*.java</exclude>
              </excludes>
              <includes>
                <include>java/com/**/*.java</include>            
              </includes>
            </configuration>
          </plugin>
          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <excludes>
                        <exclude>**/old/**/*.java</exclude>                 
                    </excludes>
                    <archive>
                        <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.start.Start</mainClass>
                        </manifest>
                    </archive>
                </configuration>
           </plugin>
        </plugins>
      </build>

When I run command mvn clean package, I get following output:

enter image description here

The source is not compiled and resultant jar is empty. All the sources I have referred for using custom directory structure with maven say that using sourceDirectory should solve the problem. But in my case it doesn't solve

EDIT

I am using custom directory structure as using standar directory structure did not work for me. Hers' my question related to that: Getting error in linking a source folder in eclipse maven

EDIT2:

I am linking source in java directory. That is, on the file system, application->java does not exist, but in eclipse through link source option, I have added the Java source folder from a different directory. Therefore it appears in eclipse. Also I have run maven commands with mvn command line as well as through eclipse.

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
  • If your platform supports symbolic links that may be a better idea with the mess you have. – Thorbjørn Ravn Andersen Mar 11 '18 at 14:58
  • 1
    Why do you use Maven if you don't follow the "convention over configuration" paradigm? This is actually what Maven is about, to NOT configure things. – Bevor Mar 11 '18 at 14:59
  • @Bevor I tried to use conventions but am stuck in using it conventionally. See my other question: https://stackoverflow.com/q/49186072/2458858 . I am unable to make it work the conventional way either. – tryingToLearn Mar 11 '18 at 15:04
  • Is your goal to make it working the conventional way or this way? if you create the project with `mvn archetype:generate -DgroupId=com.yourdomain -DartifactId=application -DarchetypeArtifactId=maven-archetype-archetype` it works out of the box. (btw. usually mvn install instead of mvn package is used) – Bevor Mar 11 '18 at 15:25
  • @Bevor. Either way. I am stuck in both the directions. What I cannot do is change the project structure in repo. So I was trying to do things mentioned in the other question. – tryingToLearn Mar 11 '18 at 15:27
  • 1
    I'm able to get this working with a class not in src/main/java but in java and without any maven settings (but Eclipse creates a package 'application' in my java folder when I try to add a class in there), just with a simple pom: ` 4.0.0 com.yourdomain application 1.0-SNAPSHOT ` – Bevor Mar 11 '18 at 15:39
  • ..this creates an application-1.0-SNAPSHOT.jar in my target, containing the file from the java folder. – Bevor Mar 11 '18 at 15:42
  • But how is it different from mine? – tryingToLearn Mar 11 '18 at 16:25
  • I strongly recommend to follow the conventions in Maven to make your life easier..I don't understand why you change that and what kind of problem you are trying to solve... – khmarbaise Mar 11 '18 at 19:40
  • @khmarbaise yes I tried that too but it didn't work either: https://stackoverflow.com/questions/49186072/getting-error-in-linking-a-source-folder-in-eclipse-maven – tryingToLearn Mar 12 '18 at 04:45
  • @Bevor Does the main class need to be in the directory specified by `sourceDirectory`? – tryingToLearn Mar 13 '18 at 07:42
  • From the logical point of view I guess so. Otherwise Maven will not find it. But I'm not sure if your pom is working. I can give you an example tomorrow when I'm at home (if I don't forget). – Bevor Mar 13 '18 at 09:22
  • @Bevor I put the question in wrong way. I meant does the main class need to be at the root of source directory? – tryingToLearn Mar 13 '18 at 17:21

3 Answers3

4

If I understand your issue correctly, You have an application folder and the actual source (java) folder is from somewhere else in the file system. And you linked the external folder as java source through eclipse for compilation.

By linking in Eclipse, maven will not automatically know where the source files are. Maven follows standard directory structure for looking up java and test files. You can use this Build Helper plugin to customize the way maven looks up sources.

An example talked here

Anil Bachola
  • 289
  • 2
  • 5
3

This is your problem: <include>java/com/**/*.java</include> You should not include the sourcedirectory, just the paths relative to it.

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
3

Follow these steps of this working example and compare it step by step with your project to figure out what's wrong:

  • create a folder.
  • create inside the folder a pom.xml with the following content:

<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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.yourdomain.yourproject</groupId>
 <artifactId>yourapp</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>yourapp</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.javadoc.skip>true</maven.javadoc.skip>
 </properties>

 <dependencies>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.9</version>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.5</version>
   </plugin>
   <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
     <archive>
      <manifest>
       <mainClass>com.yourdomain.yourproject.content.Main</mainClass>
      </manifest>
     </archive>
     <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
     </descriptorRefs>
     <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
     <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
       <goal>single</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>2.5.1</version>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>

    <dependencies>
     <dependency>
      <groupId>org.jvnet.wagon-svn</groupId>
      <artifactId>wagon-svn</artifactId>
      <version>1.12</version>
     </dependency>
    </dependencies>

   </plugin>
  </plugins>
 </build>
</project>
  • create this folder structure inside your root folder:

src/main/java/com/yourdomain/yourproject/content/

create a Main.java in content folder with the following content:

package com.yourdomain.yourproject.content;

public class Main
{
    public static void main(String[] args)
    {
        System.out.println("HELLO");
    }
}
  • go back to your root folder and execute mvn clean install
  • a target folder will be created with the jar in there.
  • you can execute it with java -jar target/yourapp-1.0-SNAPSHOT.jar
Bevor
  • 8,396
  • 15
  • 77
  • 141