What exactly do the Maven build... choice inside the Eclipse Run As? - java

I am absolutely new in Maven and I am studying it. I am using Eclipse
I have the following doubt: into Eclipse I have a Maven project.
If I select this project and I do right click on it and then I select the Run As option I have some choice. What exactly represent these choice? Are Maven tasks or what?
In particular to perform my project I have 2 possible choices:
Maven build
Maven build...
What exactly does these tasks? It seems to me that build the project and perform the related unit test. Is it true or am I missing something?
What is the difference between the previous choices. In the tutorial that I am following it is used the Maven build... and then set the clean package as goals. What exactly do this goal?
Tnx

The Developing with Eclipse and Maven - 4.2. Running Maven Builds doesn't really give any clue about the "Maven build" command.
So:
the "Maven build..." allows to define a new run configuration
the "Maven build" allows to choose between the existing user's run configurations or to create a new one if none exists.
About a user's run configuration with "clean package": have you ever used Maven? May be a good tutorial would be a first step... In the mean time - those goals will:
delete the target sub folder
rebuild the project in the new empty target sub folder
generate the archive jar-s as specified in the project pom

Related

Is it necessary to always select 'update project' before selecting run as 'maven build...' in Eclipse?

Do I have to go update project first before run as maven build in Eclipse? (Refer to pics)
Update Project as seen in picture
Run as Maven build... option as seen in picture
Can I just select the run as Maven build... option? Will there be any implications if I did that?
No, it is not required to run update project before building with "Run as Maven build ...".
You usually use update project to sync the Eclipse project with the content of the pom.xml. So you use it after changing something in the pom.xml.
See also this question:
What does Maven Update Project do in Eclipse?

How to configure default project build in IntelliJ [duplicate]

Case in question: A Maven based project that has a Maven Run Configuration with its 'Command line' parameters defined as clean install.
Clicking the green arrow to invoke 'Run my_maven_config' downloads all necessary packages from Nexus and completes the build successfully.
Selecting 'Build Project' however, invokes a seemingly similar process but after building successfully for quite a while, it fails on a missing Hibernate package.
Why is this difference?
Shouldn't the build part of 'Run' be identical to the "standalone build"?
They are not identical. IntelliJ IDEA imports the external project model from Maven and converts it to the internal project model. It's not 100% accurate, some Maven plug-ins are not supported, some dependencies may fail to resolve, etc.
See this answer to perform the diagnostics. Try re-importing the project (delete .idea directory first if it doesn't help).
Delegating build/run actions to Maven as it can be done now with Gradle will be supported later (probably in IntelliJ IDEA 2018.3).
Now IntelliJ IDE allow to delegate Build actions to maven.
Go to Preferences and search for "Delegate IDE build actions to maven".
So when you build a project, IntelliJ IDEA invokes the appropriate Maven goals.

How to build artifacts from IntelliJ via command line

I am looking to make my development life a little easier. Currently I have to go through multiple steps to deploy my code base to a test environment to allow for QA to do their work. These steps are not difficult just that I want to automate it so that it can auto run twice a day.
One thing that I am having trouble figuring out what to do is to automate my build artifacts phase in IntelliJ. I have two modules in my IntelliJ projects and I build artifacts for one of them.
The steps I go to build the artifacts are
Build --> Build artifacts --> Select Artifact to build
I was wondering if something like Ant would be able to do the job? I am not familiar with Ant, so I thought I was ask people opinions on it first.
Ant can do what you want, but personally I prefer Gradle or Maven to build my projects. You can use them even to deploy your app into test servers. Here's a simple tutorial for you to start learning gradle (my favorite one at the moment)
Usually what I do is have my run/debug also build my artifacts. On the bottom of "Run/Debug Configurations" in Intellij you can add "Build Artifact" to "Before launch: Make, Activate tool window".
You can probably build the artifact with ant by generating build.xml through Menu --> Generate Ant Build but would have to keep it updated. Maven or Gradle is a better option in this regard.
Ideally though, you would want a CI tool like Jenkins (there are others) to deploy your code to your environments. So when you push (with your VCS) or trigger it, everything is built by Jenkins and sent to where it needs to be for the QA team.
http://codurance.com/2014/10/03/guide-to-deploying-artifacts-with-jenkins/
To build Artifact from command line I used Ant.
These are the steps:
Install Idea Ant Generator Plugin
https://plugins.jetbrains.com/plugin/14169-ant-build-generation
and use "generate ant build ..." to create an ant xml file of your project (and modules).
Open Ant tool window and add the file generated.
Click build.all.artifact to test the build
Now download Ant from Apache repository https://downloads.apache.org/ant/binaries/
Unzip and add the folder to your SYSTEM PATH.
Now from command line:
ant -buildfile myfileant.xml build.all.artifacts

How to build and run Maven projects after importing into Eclipse IDE

I am learning building a Java project in Eclipse using Maven. I created a Java project HelloWorld from
“maven-archetype-quickstart” template in a folder D:/maven_projects. Then to convert the Maven project to support Eclipse IDE, I navigated into the project folder and issued the commands:
mvn eclipse:eclipse and mvn package .
Then I imported the project in Eclipse and did the necessary Eclipse configurations like setting the Maven local repository in Eclipse classpath. Now the project in D:/EclipseWorkspace folder. I ran the project successfully in Eclipse printing "helloworld".
Now if I want to go on develop the project and for that reason want to add new dependencies in pom.xml in Eclipse, then the new jars are not added in classpath when I run the project.
So my question is after importing a Maven project into Eclipse how can I add more and more dependencies in pom.xml, then build and run the project? What is the recommended and efficient way to do this?
I would recommend you don't use the m2eclipse command line tools (i.e. mvn eclipse:eclipse) and instead use the built-in Maven support, known as m2e.
Delete your project from Eclipse, then run mvn eclipse:clean on your project to remove the m2eclipse project data. Finally, with a modern version of Eclipse, just do "Import > Maven > Existing project into workspace..." and select your pom.xml.
M2e will automatically manage your dependencies and download them as required. It also supports Maven builds through a new "Run as Maven build..." interface. It's rather nifty.
1.Update project
Right Click on your project maven > update project
2.Build project
Right Click on your project again. run as > Maven build
If you have not created a “Run configuration” yet, it will open a new configuration with some auto filled values.
You can change the name. "Base directory" will be a auto filled value for you. Keep it as it is. Give maven command to ”Goals” fields.
i.e, “clean install” for building purpose
Click apply
Click run.
3.Run project on tomcat
Right Click on your project again. run as > Run-Configuration.
It will open Run-Configuration window for you.
Right Click on “Maven Build” from the right side column and Select “New”.
It will open a blank configuration for you.
Change the name as you want. For the base directory field you can choose values using 3 buttons(workspace,FileSystem,Variables). You can also copy and paste the auto generated value from previously created Run-configuration. Give the Goals as “tomcat:run”. Click apply. Click run.
If you want to get more clear idea with snapshots use the following link.
Build and Run Maven project in Eclipse
(I hope this answer will help someone come after the topic of the question)
Dependencies can be updated by using "Maven --> Update Project.." in Eclipse using m2e plugin, after pom.xml file modification.
Just install the m2e plugin for Eclipse. Then a new command in Eclipse's Import statement will be added called "Import existing maven projects".
answer 1
Right click on your project in eclipse
go to maven -> Update Project
answer 2
simply press Alt+F5
after updating your pom.xml. This will build your project again and download all jar files
Right Click on your project
Go to Maven>Update Project
Check the Force Update of Snapshots/Releases Checkbox
Click Ok
That's all. You can see progression of build in left below corner.
When you add dependency in pom.xml , do a maven clean , and then maven build , it will add the jars into you project.
You can search dependency artifacts at http://mvnrepository.com/
And if it doesn't add jars it should give you errors which will mean that it is not able to fetch the jar, that could be due to broken repository or connection problems.
Well sometimes if it is one or two jars, better download them and add to build path , but with a lot of dependencies use maven.
If you are getting this error :in cucumber Exception
Cucumber Exception: java.lang.ClassNotFoundException:
cucumber.io.ResourceLoader :
then add following jar file to your pom.xml
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.1.8</version>
</dependency>
Run the maven build once and should be gone

How to use Mercurial, Maven and Eclipse together?

I am currently working on a Java project within a team of 5 colleagues for university. Actually, we will finish the planning phase within the next few days and then start implementing.
For the project, we have to use Mercurial (via Bitbucket.org) and Apache Maven. We'd like to use Eclipse as IDE. I know how to use Mercurial, and I've read some articles and guides about Maven. The thing I don't understand is how we should use those tools in collaboration.
What should be placed on the repository? The whole Eclipse project? Or just the source files and Maven's pom.xml? If the latter, how would a working session look like? Pull the files, create an Eclipse project with mvn eclipse:eclipse, code a while and commit/push them to the repo?
I am responsible for creating the project structure, so my colleagues - and me too - can start developing. But I really don't know how to start now. If the use of Maven wasn't obligatory, I would just place the Eclipse project into the repository. But having to use it, I'm quite confused now how if affects everything.
I use a similar setup, except I am using Dropbox for my repository (just two developers). Just follow these steps, with the m2eclipse (update site: http://m2eclipse.sonatype.org/sites/m2e) and the MercurialEclipse (update site: http://cbes.javaforge.com/update) plugin you can do all in eclipse.
Create a maven project:
File -> New -> Other -> Maven project
In Project explorer right click on the new created maven project
Team -> Share Project... -> Mercurial -> (leave the folder as it is) Finish
Then create an .hgignore file in the project root:
In Project explorer right click on the new created maven project
New -> File -> File Name: .hgignore and Finish
.hgignore:
syntax: regexp
target # maven output
\.classpath # eclipse
\.project # eclipse
\.settings # eclipse
test-output # eclipse junit/testng plugin output
Then you can make your first commit, but beware, hg does not store empty folders in it's repository, so to keep the maven folder structure with no source code in it you have to put a (empty) file in each empty folder, for a simple maven project without any sources it would be in the folders:
myproject/src/main/java
myproject/src/main/resources
myproject/src/test/java
myproject/src/test/resources
For example I put a file with the name .empty in it with the content "this is a placeholder file, remove if other files are in this folder"
If some of this folders are missing your colleagues would see errors in eclipse if they import your project.
First commit:
In Project explorer right click on the new created maven project
Team -> Commit... -> Select all and enter a commit message -> Finish
Now you can clone it to bitbucket (I don't have experience with bitbucket).
Tell your colleagues to install the two eclipse plugins and then they can get the repo via
File -> New -> Other -> Check out Maven Project from SCM -> enter url to your repo -> Finish
And then you are good to go.
Here are alternatives based on practices at the ASF (though we don't use Mercurial).
To begin with, set up a source tree with Maven build that you check into Hg. Don't touch eclipse yet. You'll want to find some Eclipse plugin for Hg.
Then, look at two alternatives: the maven-eclipse-plugin and M2eclipse.
The maven-eclipse-plugin is a maven plugin. You run it from command line, and it generates .project and .classpath and some of .settings. It's not currently being worked on, but it's quite stable and functional in many common cases. You then import 'existing Eclipse project' and you are good to go.
M2Eclipse is an Eclipse plugin that integrates Maven builds. It's quite ambitious. However, it's current avatar is quite new, and can be full of surprises.

Categories