Maven: separate "Build" from "Run" in Eclipse? - java

I am working with my first Maven project in Eclipse. I want Maven to build (run configuration is of type Maven Build) my jar file and everything additional to ${project.basedir}/build. I configure my pom.xml as follows.
<build>
<directory>${project.basedir}/build</directory>
Now I have the problem that when I want to Run (not build, so the run configuration is of type Java Application) then the ${project.basedir}/build directory is being used as current working path.
How to I separate directories for build and run in Maven (Eclipse Plug-in)?

Related

How do I export Maven Libraries together with my program?

I've created a Discord bot in Eclipse and want to export it (Jar file). The problem is: I can't run it. Each time I try to start it it gives me this error: java.lang.NoClassDefFoundError: net/dv8tion/jda/core/entities/Game
I'm using "JRE System Library [JavaSE-1.8]" for my program.
This here is my pom.xml file
https://pastebin.com/bcs51jm9
After some research it seems like Eclipse doesn't implement the Maven Librarys into the Jar-file.
How can I fix this?
Exporting a jar with Eclipse from the wizard is generally the way when you don't have a build tool that allows to do it in a straight way.
You use Maven. So you have this tool.
What you need is configuring your pom.xml to enable the assembly plugin execution with the jar-with-dependencies descriptorRef.
It allows to specify the main class and to create a fat jar that contains all required dependencies at runtime according to the Maven dependencies declaration defined in your pom.
By executing mvn assembly:single you could so generate an additional jar : a jar-with-dependencies.

Exporting Spring Boot application as JAR file in eclipse

I'm using the Spring STS in Eclipse to create a simple web-based spring boot project. I can run it fine in Eclipse, but when I try to export it as a JAR file I get:
rg.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
My public static void mainis located in Application.java, with the #SpringBootApplication annotation.
I've double checked all the Maven dependencies a hundred times.
What am I doing wrong?
Most likely, you're using the built-in Eclipse exporter to generate your jar, which only includes the target files actually produced in that project. In order to have a "fat" (standalone executable) jar, you need to use the Spring Boot Maven or Gradle plugin to "repackage" the jar.
First, make sure that you have the repackage goal included in your build setup, then use the Maven package target. The simplest way to do this is to run mvn package from the command line (you may need to install the Maven CLI package for your OS); you can also right-click the POM in Eclipse and "Run As" to execute specific Maven operations from within Eclipse.
It is a single line command, on window 7/10 machine, with command prompt to your project folder (Inside your project workspace). I do not do with Eclipse IDE POM maven goals, but you can do with maven goal there also. ON window machine I prefer cmd.exe for exporting and running.
mvnw clean package
on unix kernel based
./mvnw clean package
You have to go inside workspace and than to the project root folder. You will see a maven wrapper mvnw, with that you don't need to have maven installed and .mvn folder at the same level provides necessary jar for that.
For a project
D:\workspace\Zuteller Workspace\zusteller>mvnw clean package
it will create zusteller-0.0.1-SNAPSHOT.jar in the target folder at the same level.
D:\workspace\Zuteller Workspace\zusteller>java -jar target\zusteller-0.0.1-SNAPSHOT.jar
You can run self-contained application(embedded Tomcat) and access at localhost:8080/your project

Jar missing generated pom.properties file when built in Eclipse

My project refers to the generated pom.properties file that Maven generates in:
META-INF/maven/${groupId}/${artifactId}
Which is included (in a Jar) as a dependency in another project. When I build / package the main project with Maven outside of Eclipse, the dependency Jar is built as expected (containing the pom.properties file) and all is well.
However, when I build / run the same (parent) project within Eclipse, it's not there. What am I missing?
Using Eclipse, a Jar library of the dependency is there (in the lib folder), containing everything except the Maven generated files. So in my case:
META-INF/
META-INF/persistence.xml
META-INF/MANIFEST.MF
~~~ snip ~~~
The dependency project exists in the same workspace and I have "Workspace Resolution" enabled.
Looking at the build target folder in Eclipse (\target\classes), I can see all the files that Eclipse uses for the Jar, so I can only assume that Eclipse treats it as a "regular" Java project (despite the parent project listing it as a Maven dependency in the POM) - so Maven doesn't get involved in the packaging.
How can I get Eclipse to treat the project dependency as a Maven project / so that the generated pom.properties will be included in the Jar used by the parent project?
Update
For the parent (War) project, the following structure is generated whenever I use Eclipse to build the project (using "Build Project" or "Build Automatically" not "Maven build"):
target\m2e-wtp\web-resources\META-INF\maven\${groupId}\${artifactId}\pom.properties
This is referenced in the Eclipse deployment assembly:
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
So that when the War is deployed to Tomcat, the META-INF\maven... structure exists at the root of the archive.
The dependency / Jar project obviously has the same "Deployment Assembly" facility in Eclipse, so I just need to figure out what creates the m2e-wtp folder on build and apply it to that project too.
Actually, I don't think you can with the default Eclipse Jar-Export.
The best you can do if you want to do it from within Eclipse is to have a Maven Build run configuration which doesn't do much differently than using Maven outside Eclipse. But at least you have a button in Eclipse to click do generate the jar, if that's the main reason. But the normal Eclipse functionality to produce a jar (File > Export...) ignores all Maven settings.
Workspace Resolution only works for compiling and running code, but not for other Maven functionality.

Java build path not updated with pom dependencies

I updated the pom.xml to spring 3.0.1.RELEASE instead of Spring 2.x.x .
So when I did 'mvn clean install', the lib directory in my target directory was correctly updated with the right jar files.
But when I do a right click on my project and then go to "Build Path", I notice that I have the old dependencies of spring and not the dependencies for spring 3.0.
How can I update the Java Build Path?
If this is eclipse (you are using), then you need to update the Eclipse project file from the .pom file. That is done by doing the following command:
mvn eclipse:eclipse
After that, refresh project in eclipse and you will see that eclipse recognises the dependencies from the pom file.

migration an Maven projet from eclipse to a command line environment

I got an Maven project which is compiled in Eclipse. Now I need to migrate it to a Linux environment, and there won't be GUI interface I can use. I wonder what I should do to migrate it?
Currently , under Eclipse project folder I have the files/folders as the below:
.classpath
.project
.settings
.springBeans
doc
pom.xml
src
target
I figure all those .* folders are Eclipse meta data. so I can remove them. Then I can use the rest to form a Maven project that I can build using Maven command lines?
For a maven project to work command line, all you need is
Maven is installed and correctly exported in the $PATH variable
The pom.xml in your workspace.
Just go to the project directory and run mvn install.
mvn clean
then remove .project .classpath
if you want to lose eclipse project settings while migrating delete .settings (assuming it doesn't have machine specific path/settings)
copy rest to new linux environment
open eclipse, import project as maven project (assuming you have new eclipse with maven plugin setup on linux environment)
Note:
make sure you still have a backup before you successfully migrate over
.classpath contains references to local .m2 when used with eclipse and maven so the path would differ in linux and would create issue
.project contains some configuration that is eclipse maven plugin dependent, so it is good to loose it once and let new environment create new one

Categories

Resources