简体   繁体   English

Maven自定义目录结构 - 尽管指定了sourceDirectory,但没有可编译的源代码

[英]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. 我正在使用自定义目录结构并在sourcedirectory标记中指定了该目录。 But still I get the message No sources to compile . 但我仍然收到消息No sources to compile Although the build is successful. 虽然构建成功。

My directory structure: 我的目录结构:

在此输入图像描述

So instead of src/main/java , I am using java . 所以不是src/main/java ,而是使用java (And I have a reason to do that, so right now it's not possible to switch to src/main/java ) (我有理由这样做,所以现在不可能切换到src/main/java

Here's my pom.xml: 这是我的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: 当我运行命令mvn clean package ,我得到以下输出:

在此输入图像描述

The source is not compiled and resultant jar is empty. 源代码未编译,结果jar为空。 All the sources I have referred for using custom directory structure with maven say that using sourceDirectory should solve the problem. 我提到的使用maven自定义目录结构的所有源代码都说使用sourceDirectory可以解决问题。 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 她的问题与此有关: 在eclipse maven中链接源文件夹时出错

EDIT2: EDIT2:

I am linking source in java directory. 我在java目录中链接源代码。 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. 也就是说,在文件系统上,application-> java不存在,但是在eclipse中通过链接源选项,我已经从不同的目录添加了Java源文件夹。 Therefore it appears in eclipse. 因此它出现在日食中。 Also I have run maven commands with mvn command line as well as through eclipse. 我还使用mvn命令行以及eclipse运行maven命令。

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. 如果我正确理解您的问题,您有一个应用程序文件夹,实际的源(java)文件夹来自文件系统中的其他位置。 And you linked the external folder as java source through eclipse for compilation. 并且您通过eclipse将外部文件夹作为java源链接以进行编译。

By linking in Eclipse, maven will not automatically know where the source files are. 通过在Eclipse中链接,maven不会自动知道源文件的位置。 Maven follows standard directory structure for looking up java and test files. Maven遵循标准目录结构来查找java和测试文件。 You can use this Build Helper plugin to customize the way maven looks up sources. 您可以使用此Build Helper插件来自定义maven查找源的方式。

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: 在文件夹内创建一个包含以下内容的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/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/ 的src / main / JAVA / COM / YOURDOMAIN / yourproject /内容/

create a Main.java in content folder with the following content: 使用以下内容在content文件夹中创建Main.java

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 返回到您的根文件夹并执行mvn clean install
  • a target folder will be created with the jar in there. 将在那里使用jar创建目标文件夹。
  • you can execute it with java -jar target/yourapp-1.0-SNAPSHOT.jar 你可以用java -jar target/yourapp-1.0-SNAPSHOT.jar来执行它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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