简体   繁体   English

Maven和Intellij的构建方式不同

[英]Maven and Intellij builds differently

I have a parent pom. 我有一个父母pom。 When I am on the parent and write mvn clean install my dropwizard application builds fine. 当我在父母那里写mvn clean install我的dropwizard应用程序构建良好。

However, in Intellij it fails because it cannot find my config.yml file. 但是,在Intellij中失败,因为找不到我的config.yml文件。

The problem is that I need to include my module directory in Intellij, but not in Maven. 问题是我需要在Intellij中包括我的模块目录,但不在Maven中。 Why is that? 这是为什么?

Running mvn install this works 运行mvn install这工作

public static void main(String[] args) throws Exception {
    new CivilizationApplication().run(new String[]{"server", "src/main/resources/config.yml"});
}

In Intellij I must change to the following 在Intellij中,我必须更改为以下内容

public static void main(String[] args) throws Exception {
    new CivilizationApplication().run(new String[]{"server", "civilization-rest/src/main/resources/config.yml"});
}

How can I configure Intellij, so it will work without specifying the module directory? 我该如何配置Intellij,这样它就可以在不指定模块目录的情况下工作?

You could try setting your working directory in your run configuration... 您可以尝试在运行配置中设置工作目录...

在此处输入图片说明

I would also look into your pom.xml configuration to see if you can change the working source sets to use this directory upon importing the maven project. 我还将调查您的pom.xml配置,以查看是否可以在导入maven项目时更改工作源集以使用此目录。

Set Working directory to $MODULE_DIR$ . Working directory设置为$MODULE_DIR$ Maven always uses $MODULE_DIR$, but you probably do not have $MODULE_DIR$ by default for IntelliJ Application executions. Maven始终使用$ MODULE_DIR $,但是默认情况下您可能没有$ MODULE_DIR $用于IntelliJ Application执行。

在此处输入图片说明

You can just tell Intellij to do exactly the same thing Maven does when you start it. 您可以告诉Intellij启动Maven时完全相同。

You can do so by clicking on the drop down menu next to the "Launch App" button. 您可以通过单击“启动应用程序”按钮旁边的下拉菜单来实现。 There you can define a new Run configuration that uses Maven by clicking on the + to the left and then choosing Maven. 您可以在此处定义新的使用Maven的运行配置,方法是单击左侧的+,然后选择Maven。

Then you can just enter clean install exec:java where it says "Command line" and it should work exactly like it does when you execute it manually from the command line. 然后,您可以只输入clean install exec:java并显示“命令行”,它的工作方式应与从命令行手动执行时的工作方式完全相同。 Debugging should work like this as well without any problem. 调试也应该像这样工作,没有任何问题。

在此处输入图片说明

The problem here is that you are refering to a resource file by using a relative path instead of using the classpath. 这里的问题是您通过使用相对路径而不是使用类路径来引用资源文件。

So, instead of 所以,代替

new CivilizationApplication().run(new String[]{"server", "src/main/resources/config.yml"});

You better have this 你最好有这个

new CivilizationApplication().run("server", getClass().getResourceAsStream("config.yml"));

(obviously, make sure to change the signature of the #run() method as well) (显然,还要确保也更改#run()方法的签名)

Just a thought but maybe it's to do with the Resource not being copied. 只是一个想法,但可能与资源未被复制有关。

I'm on OSX so the exact terms may differ if you're on Windows but for me... in Preferences, select "Build, Execution, Deployment", then "Compiler", and try adding ";?*.yml" to the Resource patterns. 我使用的是OSX,因此如果您使用的是Windows,则确切的术语可能有所不同,但对我而言...在“首选项”中,选择“构建,执行,部署”,然后选择“编译器”,然后尝试添加“;?*。yml”资源模式。

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

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