Import library in Java project via - java

I was looking for a Random name generator and found https://github.com/DiUS/java-faker on GitHub. There it says:
In pom.xml, add the following XML stanza between <dependencies> ... </dependencies>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.16</version>
</dependency>"
I am not familiar with Maven or Gradle or how it works, nor did the documentation of Maven really help me with importing this library into my Java project in Eclipse.
Would be thankful for any guidance (links) towards the documentation/info I need in order to understand how to use this library via either Maven or Gradle. Is this actually that complicated? Why can I not just download and add the library to my build path?

Gradle
Make sure you have Gradle installed by running gradle -version.
Create a directory for your project and navigate into it. Open a terminal.
Execute gradle wrapper. You'll see gradlew and gradlew.bat files and gradle and .gradle directories created. From now on, you can forget about global Gradle installation as you can use wrapper. Wrapper can be used even when Gradle is not installed.
Create a file named build.gradle. It's a project descriptor in Gradle:
plugins {
id 'java'
}
repositories {
jcenter()
}
dependencies {
implementation("com.github.javafaker:javafaker:0.16")
}
Import the project in Eclipse.
Maven
Make sure you have Maven installed by running mvn -version.
Create a directory for your project and navigate into it.
Create a file named pom.xml or build.gradle. It's a project descriptor in Maven:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.your.company</groupId>
<artifactId>app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.16</version>
</dependency>
</dependencies>
</project>
Import the project in Eclipse.
Is this actually that complicated?
As you see, no, it's not complex at all!
Why can I not just download and add the library to my build path?
You can. This approach just don't scale when you have dozens and hundreds of dependencies (with their own dependencies). Plus, modern software needs not only to be compiled, but tested, packaged, released & distributed. Though you can do most of that from your IDE, but… This appriach just don't scale. Just because people use different IDEs. Because there is no IDE on a build server. Because when you know Gradle or Maven a little bit better, you'll see that it's even faster to accomplish tasks via build tool then via menu items.
Happy hacking!

Related

Creating eclipse source files src/main/java and src/main/groovy with maven

I am trying to get a specific kind of file structure in eclipse for my work.
This is the structure I want:
The problem I am having is that everytime I use mvn eclipse:eclipse the structure of the files gets broken like this:
I have tried most of the answers that I've found on Stack Overflow and elsewhere. I've tried making the fileStructure2 to a maven Archetype and it sort of worked, but as soon as I ran mvn eclipse:eclipse it broke again:
I learned the basics of the archetype creation just today so I am not a master at that either and there might be something that is missing from the creation of the archetypes. I used mvn archetype:create-from-project in the FileStructure2 directory and then used mvn install archetype:update-local-catalog in the archetype directory which resulted the archetype to be added in the list of archetypes and I just created a new project with cmd and then ran mvn eclipse:eclipse which resulted in the above picture. I have not changed anything in the archetype folders that were created, I just used the basic ones that were created with running the archetype:create.
Most of the other posts i've found are really old and none of them have the structure I want, or it is only for src/main/java which I get working as intended but it is just the src/main/groovy file that switches the location to a normal folder instead of a source folder.
I am thinking it is my pom.xml that needs some modifications. at the moment it looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>FileStructure</groupId>
<artifactId>FileStructure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.5.0</version>
<type>pom</type> <!-- required JUST since Groovy 2.5.0 -->
</dependency>
</dependencies>
</project>
I have not added any maven compiler plugins since I don't 100% know what they do and still break the structure no matter what I've tried.
I am using apache-maven-3.8.5 which is downloaded with chocolatey. My eclipse jre and jdk versions are 1.8.0_333 but my java home is jdk11 since sonarQube can't use that old versions. My groovy is downloaded from the eclipse marketplace and it's compiler version is 2.5. My eclipse is a SAP netweaver. Eclipse Java EE IDE for Web Developers. Version: Oxygen.3a Release (4.7.3a)
I have converted the projects to maven and groovy via Eclipse's own methods.
Eclipse Groovy Development Tools will do this. There is a guide here with steps to achieve your desired result:
https://github.com/groovy/groovy-eclipse/wiki/Groovy-Eclipse-Maven-plugin
There is an archetype as well but it is not pre-built: https://github.com/groovy/groovy-eclipse/tree/master/extras/groovy-eclipse-quickstart

mvn command for updating version in dependency and one project that uses the dependency

I'm trying to figure out the best practice for updating the version in a POM file that is used by several other Java projects. I'm new to Java and Maven, but not OOP and semver.
I took over a few git repos with Java code using a maven build system. I need to add new features to a "core" repo that is used by several other repos, even though I only need one of the repos to benefit from the new features. My hope is to leave the other repos untouched. If the other repos get redeployed by some devops process, I want them to use the version of "core" without any changes I added to "core".
I think I should update the version in the POM file of the "core" repo before storing its artifact in Nexus so the other repos won't download the version of "core" containing my changes. What mvn command should I use?
Right now, the "core" library declares its version like:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.team</groupId>
<artifactId>foo-core</artifactId>
<version>1.0.4-SNAPSHOT</version>
<packaging>jar</packaging>
<name>foo-core</name>
<url>http://maven.apache.org</url>
<blah/>
<blah/>
</project>
The projects that use "core" have a POM that looks like:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.team</groupId>
<artifactId>bar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Bar</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>com.company.team</groupId>
<artifactId>foo-core</artifactId>
<version>1.0.4-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
I'm not sure when <parent> is a useful tag, but I notice it is not being used to connect the application repos to the "core" repo.
I imagine I want to update the POM
for the core
and for the one project that uses the core, but not the other projects that also use the core.
I interpret the fact that the version -SNAPSHOT is used in references to "core" by all the other projects to tell me that no "release" was created for the "core", at least at the current version. Can I skip ever making a release for 1.0.4 if I want to the version of "core" container my features to be 1.0.5-SNAPSHOT?
What are the commands I need to execute in both the "core" repo and the repo for the one project I want to use the new build? I think some commands simply modify the POM, but other commands will publish the build to Nexus.
Or can I just type a new number in the POM and run a mvn command to push the build to Nexus with the new version? If I simply change the value with a text editor, I plan to leave the word -SNAPSHOT in the incremented version of "core" as well as the reference to "core" in the one app I want to use the new features. Then I will just need to publish core to Nexus.
--- UPDATE ---
I learned that this command will bump the version number in "core". It seems unnecessarily complex since you still need to type in the full version value when prompted by the CLI (I typed "1.0.5-SNAPSHOT").
mvn versions:set nestSnapshot
And I guess this is how you update the app to use the latest version of the dependency, but I think it only works if you succeeded to publish the dependency with mvn deploy.
mvn versions:use-latest-releases -Dincludes=com.company.team:foo-core -DallowSnapshots=true
You can just go to the two POM files you mentioned and change the version number in the file.
I would first change the version of the core, then run a build mvn clean install (if this done on your local machine) or mvn clean deploy (if you want to sent it to your company repository) and then change the version number of core in the other project.
Note that SNAPSHOT versions are for development. When you want to release something, create a release version, e.g. through the Maven release plugin.

Importing Java owlexplanation libraries in Eclipse [duplicate]

I don't know how to use Maven at all. I've been developing for a couple years with Eclipse and haven't yet needed to know about it. However, now I'm looking at some docs that suggest I do the following:
"To include it within your project, just add this maven dependency to your build."
<repository>
<id>jboss</id>
<url>http://repository.jboss.org/maven2</url>
</repository>
...
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>1.1.GA</version>
</dependency>
How do I do this with my Eclipse project?
Please assume I know nothing about Maven. I just figured out it might be installed on my computer by typing mvn on the command line, but that's seriously the extent of my knowledge. I would be happy to continue knowing nothing about Maven if there is an equivalent, non-Maven way of following these instructions with Eclipse.
On the top menu bar, open Window -> Show View -> Other
In the Show View window, open Maven -> Maven Repositories
In the window that appears, right-click on Global Repositories and select Go Into
Right-click on "central (http://repo.maven.apache.org/maven2)" and select "Rebuild Index"
Note that it will take very long to complete the download!!!
Once indexing is complete, Right-click on the project -> Maven -> Add Dependency and start typing the name of the project you want to import (such as "hibernate").
The search results will auto-fill in the "Search Results" box below.
In fact when you open the pom.xml, you should see 5 tabs in the bottom. Click the pom.xml, and you can type whatever dependencies you want.
You need to be using a Maven plugin for Eclipse in order to do this properly. The m2e plugin is built into the latest version of Eclipse, and does a decent if not perfect job of integrating Maven into the IDE. You will want to create your project as a 'Maven Project'. Alternatively you can import an existing Maven POM into your workspace to automatically create projects. Once you have your Maven project in the IDE, simply open up the POM and add your dependency to it.
Now, if you do not have a Maven plugin for Eclipse, you will need to get the jar(s) for the dependency in question and manually add them as classpath references to your project. This could get unpleasant as you will need not just the top level JAR, but all its dependencies as well.
Basically, I recommend you get a decent Maven plugin for Eclipse and let it handle the dependency management for you.
Open the pom.xml file.
under the project tag add <dependencies> as another tag, and google for the Maven dependencies. I used this to search.
So after getting the dependency create another tag dependency inside <dependencies> tag.
So ultimately it will look something like this.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>doc-examples</groupId>
<artifactId>lambda-java-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lambda-java-example</name>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Hope it helps.
I have faced the similar issue and fixed by copying the missing Jar files in to .M2 Path,
For example: if you see the error message as Missing artifact tws:axis-client:jar:8.7 then you have to download "axis-client-8.7.jar" file and paste the same in to below location will resolve the issue.
C:\Users\UsernameXXX.m2\repository\tws\axis-client\8.7(Paste axis-client-8.7.jar).
finally, right click on project->Maven->Update Project...Thats it.
happy coding.
I have faced same problem with maven dependencies, eg: unfortunetly your maven dependencies deleted from your buildpath,then you people get lot of exceptions,if you follow below process you can easily resolve this issue.

Getting up and running with basic Selenium Project using Intellij-idea & Maven

I am a programmer new to Java world.
I am using Intellij-idea, Maven, & Selenium.
I followed instructions exactly to generate Selenium Project.
The link is:
http://www.seleniumhq.org/docs/03_webdriver.jsp
The directions ask you to
create project folder
drop pom.xml they gave you into Project folder
edit group & artifact tags of the Pom.xml
Run “mvn clean install” command
So I am enclosing the pom.xml I used, and I followed directions above using “FWSDriverProject” as my folder (for pom.xml file, see bottom of this message).
Since I don’t have an old version of Intellij, and the directions on the Selenium site are old, I couldn’t follow them exactly, but I did notice that a jar file was created for me in target subfolder of the project, and I am familiar on how to use jar files so I thought it worth a shot.
I created folder called C:\Java\FWS_SeleniumJarConsumer & then created simple command-line app within that folder.
I added libs folder, C:\Java\FWS_SeleniumJarConsumer\libs.
Then I dropped the JAR file created for me, into this folder.
Then, within Intellij, I opened my FWS_SeleniumJarConsumer project, and I right-clicked on the jar file and selected “Add to Library”.
I was greeted with “IDEA cannot determine what kind of files the chosen items contain. Choose the appropriate categories from the list.”
It allows you to select multiple. Since I wasn't sure what to select, I chose all of them.
I didn't get any errors.
But when trying to use the imports provided in the sample code, I get error.
import org.openqa.selenium.By;
The error say:
“Cannot resolve symbol openqa”
Any ideas on how to get basic Selenium project up and running using IntelliJ & Java?
I am using latest Maven and IntelliJ-idea. Tutorial on link is old.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>HCFCD</groupId>
<artifactId>FWSDriver</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.21</version>
</dependency>
</dependencies>
</project>
I did a similar project and I followed this tutorial to be able to create maven build and execute Selenium test in Java with IntelliJ. This should solve your problem.
Look at this video: https://www.youtube.com/watch?v=rQG_GBTFWQ4
Git repository: https://github.com/Krotov/FirstWebDriverTest
Mathieu

How to add jar file dependency in pom.xml

I have an application that depends on 2 jar file :
OperatorInterface.jar
Operator.jar
I want to build my project using Maven structure. I've followed the Maven site tutorial to create the structure like so:
mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=source.app -DartifactId=project
I've put my source file FileProcess.java in E:\project\src\main\java\source\app.
FileProcess has dependency in 2 external .jar files but I don't know how to define this dependency in pom.xml.
Here is the content of the pom.xml so far:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>source.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Can anyone help me?
First thing you need to install your jars in your maven local repository. You can follow the official tutorial here:
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
During the installation you will have to choose your own artifactId, groupId and version. You can choose whatever value you want but remember them because you will need those to include your jars in the pom.
After that you can include them in your pom.xml adding these lines under the tag dependencies for each library to include:
<dependency>
<groupId>your_group_id</groupId>
<artifactId>your_artifact_id</artifactId>
<version>your_version</version>
</dependency>
In the case when the JARs in question are your own code rather than third-party, the best approach is to "mavenize" the projects that build them as well. After you do that, running mvn install on those projects will place them in your Maven local repository where they will be available to other local Maven projects who declare them as dependencies.
Avoid adding them to yourpom.xml file straight. When you add .jars using this process, they will automatically reflect in your pom.xml
Steps are -
1. Right click on your project in the file explorer in your eclipse.
2. Go to build Path option.
3. Select configure build path
4. Chose the Libraries tab in the window that appears.
5. Add your .jar file externally.
6. Click ok and come back to your project interface
Update your maven project by pressing Alt+F5 and restart eclipse. Your problem should be solved.
I would take the following route since this is for corporate use. This is the hard and ultimately portable way that sets you up for future Maven usage as it is intended to be done.
1) make those dependent jars Maven projects (because then you can easily version-manage them too using Maven)
2) use a local repository manager and deploy your own projects to it using Maven release management through either the mvn:release plugin, or use a build server such as Hudson to automate the release process with a simple button press which I can highly recommend setting up.
https://maven.apache.org/repository-management.html
http://maven.apache.org/maven-release/maven-release-plugin/
3) mvn:release the dependency jars to your local repository manager so they will be available for other Maven projects
4) you're actually done, when you have a local repository where your deploy your own snapshot and release artifacts to, then your maven build can find your own maven modules and include them in the application dependencies - if you don't forget to configure the repository in the project's pom of course. And your build server if you have one can find them too.
The easy/lazy route is as suggested to manually install the jars in your local .m2 folder where Maven caches dependencies that it downloads, but it is absolutely not a portable solution that will stand the test of time. It won't work when somebody else needs to work on this project until they too install the jars locally. Or if its only you, you need to redo it every time you checkout the project on another computer / as another user. Also you need to update the jars each and every time you make changes to them, everywhere the project is checked out. You may need to do specific setup steps to get it working in an IDE, should you inevitably choose to start to use one.
However if you are having a time-pressure problem, then I would certainly go ahead and do that as a temporary workaround solution to be able to get going.

Categories