eclipse maven 中没有 main.java 的包名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14788480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:58:21  来源:igfitidea点击:

Package name without main.java in maven

javaeclipsemaven

提问by Grade

I develop my project in Eclipse with standard Maven directory layout. Sources are in src/main/java/com/mycompany directory. And it is required to put my source classes in package main.java.com.mycompany. When I build executable jar-file, I have to define the main class in Manifest.MF like:

我在 Eclipse 中使用标准 Maven 目录布局开发我的项目。源在 src/main/java/com/mycompany 目录中。并且需要将我的源类放在 main.java.com.mycompany 包中。当我构建可执行的 jar 文件时,我必须在 Manifest.MF 中定义主类,如:

    Main-Class: main.java.com.mycompany.MyMainClass

But I saw in a lot of examples (like configuring the mainClass attribute in maven-assembly-plugin or maven-jar-plugin) that class names are specified without main.java part. I just want to have

但是我在很多例子中看到(比如在 maven-assembly-plugin 或 maven-jar-plugin 中配置 mainClass 属性),类名是在没有 main.java 部分的情况下指定的。我只想拥有

    Main-Class: com.mycompany.MyMainClass

I can't figure out how I can achieve this because I'm completely novice in Maven.

我不知道如何实现这一点,因为我完全是 Maven 新手。

回答by verhage

Maven's default directory layout differs from the layout Eclipse uses when you create a new Java project.

Maven 的默认目录布局与 Eclipse 在创建新 Java 项目时使用的布局不同。

It's actually pretty simple. When you create a new Java project, Eclipse creates a folder named 'src' on your filesystem, where you should put your source code. Additionally, Eclipse configures this folder as a 'source folder'. Every folder you create in this folder will represent a Java package.

其实很简单。当您创建一个新的 Java 项目时,Eclipse 会在您的文件系统上创建一个名为“src”的文件夹,您应该将源代码放在这里。此外,Eclipse 将此文件夹配置为“源文件夹”。您在此文件夹中创建的每个文件夹都将代表一个 Java 包。

What Maven wants is a little bit different. Maven likes you to have a 'src/main/java' folder with the 'java' subfolder configured to be a source folder. Hence, all folders created under this java folder will represent Java packages.

Maven 想要的有点不同。Maven 希望您拥有一个“src/main/java”文件夹,其中“java”子文件夹配置为源文件夹。因此,在此 java 文件夹下创建的所有文件夹都将代表 Java 包。

You can manage your source folders in Eclipse from your project properties. Right click your project, select 'build path -> configure build path'. In the popup open the 'source' tab. Now, remove 'src' as a source folder and replace it with 'src/main/java'.

您可以从项目属性管理 Eclipse 中的源文件夹。右键单击您的项目,选择“构建路径-> 配置构建路径”。在弹出窗口中打开“源”选项卡。现在,删除“src”作为源文件夹并将其替换为“src/main/java”。

Now Eclipse will recognize your packages as com.mycompany instead of main.java.com.mycompany.

现在 Eclipse 会将您的包识别为 com.mycompany 而不是 main.java.com.mycompany。

I always use the maven-eclipse plugin to generate my eclipse project and classpath files from the commmand line. I'm not a big fan of m2e.

我总是使用 maven-eclipse 插件从命令行生成我的 eclipse 项目和类路径文件。我不是 m2e 的忠实粉丝。

回答by Ankur Kaul

You can also try updating the maven project. Right click on the project > Maven > Update Maven Project.

您也可以尝试更新 maven 项目。右键单击项目 > Maven > 更新 Maven 项目。

回答by Stephen Connolly

Use m2e if you are using eclipse. The eclipse workspace is configured incorrectly for a maven project

如果您使用 eclipse,请使用 m2e。Eclipse 工作区为 Maven 项目配置不正确