Maven build producing empty Jar
I am new to maven and I am trying to do the maven in 5 mins I have been trying to figure out why mvn package is producing an empty jar. I follow the instructions exactly as told but it doesn't work.
I did the archtype:generate goal. That created the my-app project structure in the /bin. Then when i tried to run mvn package, it said开发者_如何转开发
[INFO] Cannot execute mojo: resources. It requires a project with an existing po m.xml, but the build is not using one.
I tried several suggestions and finally tried putting the pom.xml file inside the my-app project one level higher directly in the /bin.
Now mvn package worked.
A target was created in the same directory as my-app. But the jar was empty...did not have any class files and trying to execute it threw the classnotfoundexception.
Besides is mvn supposed to work only from within /bin. Atleast thats the case with me. But I see several illustrations where they mnv commands are executed from random directories like here
Also how to create multi-module projects on maven?
Any suggestions please?
I think that you tried to run Maven in bin
directory of Maven. You should switch to my-app
directory that was generated and run it there. Maven requires very specific project structure relative to pom.xml. When you copied pom.xml, your java files and resources were left inside my-app, so Maven didn't find any and produced empty jar.
- Immediately after installing Maven, you should add it to your OS's PATH. This is an OS exercise and unrelated to maven, but we can surely help you out if you let us know what you're using.
- After doing #1, you'll be able to run maven from anywhere. That's what adding it to the PATH does. However, you'll only be able to run a build when you run it from a directory that contains a file called pom.xml (though there are command-line args that let you change this, too).
- When you generate a project from an archetype (using
mvn archetype:generate
), it creates a project directory for you and puts a pom.xml and some folders and files in it. After reading #2, you should realize that this means immediately aftermvn archetype:generate
, you need tocd <project dir>
. Then you're ready to build your maven project. - When you're in your project directory (remember, that's where the pom.xml is),
mvn package
will build a JAR (or WAR, EAR, bundle, whatever), andmvn clean
will delete all build artifacts, leaving only the source code. - Don't even worry about multimodule projects until you have the basics down. No, really until you have considerably more than the basics down. Just don't worry about it yet.
If anything is unclear, just ask.
maven is expecting source files to be present in a certain location. If your setup is messed up, which I think it is, then you will need to explicitly provide a reference to its location using element.
However, it has happened to be once even when everything was correct. Worked on Mac but generated empty jar on Linux. Adding element explicitly fixed it.
精彩评论