简体   繁体   中英

Maven and Intellij builds differently

I have a parent pom. When I am on the parent and write mvn clean install my dropwizard application builds fine.

However, in Intellij it fails because it cannot find my config.yml file.

The problem is that I need to include my module directory in Intellij, but not in Maven. Why is that?

Running mvn install this works

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

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?

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.

Set Working directory to $MODULE_DIR$ . Maven always uses $MODULE_DIR$, but you probably do not have $MODULE_DIR$ by default for IntelliJ Application executions.

在此处输入图片说明

You can just tell Intellij to do exactly the same thing Maven does when you start it.

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.

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. 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)

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.

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