简体   繁体   中英

Maven custom directory structure - no sources to compile despite specifying sourceDirectory

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:

在此输入图像描述

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:

在此输入图像描述

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.

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

这是你的问题: <include>java/com/**/*.java</include>你不应该包括sourcedirectory,只包括相对于它的路径。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM