Import an Maven dependency (Example Jsoup.jar) - maven

I have a generally understanding Problem with the Maven-Dependency in Grails 2.3.8.
I want to Import jsoup - functionality into my Project.
Therefore I did this in my BuildConfig.groovy:
dependencies {
.
.
/// jsoup
compile "org.jsoup:jsoup:1.7.3"
}
All is okay. Grails downloads the jar File into my local repo
C:\Users\xxx\.m2\repository\org\jsoup\jsoup\1.7.3
Now my confusion. I thougt all is done and i can write my code against Jsoup but this is wrong. I have to
copy the jar file into the Grails - lib Folder
set up the buildpath for Jsoup.jar
do a "grails compile"
Is this the right way? Why do i config dependencies when grails doesnt use them? It seems there is a plugin (compile ":html-cleaner:0.2") where Jsoup is included but when i need Jsoup i use Jsoup and not html-cleaner.
When i did this without my steps i got an Compiler Error:
package f
import grails.transaction.Transactional
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document
/***
*
* #author MG
*
*/
//#Transactional
class xyService {
def getXyFromIndex(String searchKeyword) {
def html = ""
Document doc = Jsoup.parse(html);
}
}
==> 'Groovy:unable to resolve class org.jsoup.nodes.Document' -GGTS 3.5.1

You don't need to copy jar it should automatically get copied either by ivy or maven. Maven is recommended so in BuildConfig.groovy change the resolver value to maven like below. Now when you start your application all jar will get copied to .m2 directory.
grails.project.dependency.resolver = "maven"

Did you try to import JSoup at the top of your file ?
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.jsoup.select.Elements
import org.jsoup.parser.Tag
#Transactional
class myClass {
}

Related

Gradle shadowJar deletes required SQL driver

I'm using the shadowJar Gradle target provided by the com.github.johnrengelman.shadow Gradle plugin to build up an application, which requires an org.apache.hive.jdbc.HiveDriver to connect to Kudu using Impala.
The problem is that when I use standard approach to import the driver in Scala:
Class.forName("org.apache.hive.jdbc.HiveDriver")
the shadow plugin removes it from the resulting JAR, implying an runtime error of:
java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver.
My build.gradle contains:
dependencies {
implementation {
"org.apache.hive:hive-jdbc:1.2.1"
}
}
How do I instruct the shadow plugin not to delete the required dependency injected via a String?
I found out the solution is to include the static type not via Class.forName, but using code by importing it:
import java.sql.DriverManager
import org.apache.hive.jdbc.{HiveConnection, HiveDriver}
class Foo {
// Register Hive Driver this way to prevent shadowing to cut it off
new HiveDriver()
DriverManager.getConnection(connectionUrl, user, password) match {
case connection: HiveConnection =>
...
}
}
This way, the shadow plugin is officially informed about the fact that the Driver is actually required.

tell cucumber how to use camelCase

I am using Maven and cucumber in test automation.
How can I use camelCase while writing my codes as cucumber is using snakeCase by default.
Is there a way to manage it in pom.xml file in maven?
btw, my IDE is VS Code.
Tnx
If you are using cucumber-junit you can add #CucumberOptions to your JUnit4 runner class.
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import static io.cucumber.junit.CucumberOptions.SnippetType.CAMELCASE;
#RunWith(Cucumber.class)
#CucumberOptions(snippets = CAMELCASE)
public class RunCucumberTest {
}
You can also create a cucumber.properties file in src/test/resources containg:
cucumber.snippet-type=camelcase
If you're using cucumber-junit-platform-engine this file should be name junit-platform.properties.
The step definitions are using snake case rather than camel case. We just need to tell Cucumber that's what we want:
java -cp "jars/*" cucumber.api.cli.Main -p pretty --snippets camelcase features
Now when we run ./cucumber it generates snippets with method names that conform t the Java standard
from The Cucumber for Java book

In a Java file, how do I import Java packages installed via Maven via Clojure's project.clj dependencies list?

I have a Clojure/Lein project, and I am successfully using a Java class I created (in ./src/com/mypackage/MyClass.java) in my Clojure code via the usual Java interop paradigm. But now I want to enhance MyClass, so in MyClass I want to import a Java package I installed via Maven via the project.clj dependencies list, but import javax.mail.Message and the like give an error: "The import javax.mail cannot be resolved". I'm admittedly very green at Java and the JVM. How can I get MyClass to know about stuff I installed via Maven via the project.clj dependencies list.
(I am already successfully using Java packages I installed via Maven via the project.clj dependencies list in my Clojure code.)
Here are my dependencies from project.clj:
:dependencies [[org.clojure/clojure "1.10.0"]
[clj-http "2.0.0"]
[cheshire "5.9.0"]
[org.jsoup/jsoup "1.8.3"]
[javax.mail/javax.mail-api "1.6.0"]
[com.amazonaws/aws-java-sdk "1.11.714"]]
Please update your question with the contents of your project.clj file.
As a guess, you may have the wrong format for the dependencies. It should look like:
[javax.mail/javax.mail-api "1.6.0"]
You can see the details at Maven Central. How should I find the right coordinates? Just google the words
maven central javax.mail.Message
and it will take you to the correct Maven Central page.
Update:
Here is a java sample file that works:
package demo;
import javax.mail.Message;
public class Calc {
public static Message msg;
public static void show() {
System.out.println( msg );
}
}
and the :dependencies from project.clj (notice the suffix CLJ, not CLI):
:dependencies [
[org.clojure/clojure "1.10.1"]
[prismatic/schema "1.1.12"]
[tupelo "0.9.173"]
[javax.mail/mail "1.4"]
;[javax.mail/javax.mail-api "1.6.0"] ; also works
]
and results:
~/expr/demo > lein clean; lein test
Compiling 2 source files to /home/alan/expr/demo/target/default+test+test/class-files
lein test _bootstrap
-------------------------------
Clojure 1.10.1 Java 13
-------------------------------
lein test tst.demo.core
Calling Calc/show
null
Ran 2 tests containing 0 assertions.
0 failures, 0 errors.

Import java class to Jsp in Maven project

I have a project in IDE, so I made a new project in Maven. I copy all the files and classes, and I fill the pom.xml.
This is OK, but the problem is in JSP,eclipse dont find the imported clases.
<%#page import="modelos.Mascota"%>
This import for example cant be resolved.
What I'm doing wrong?
All my java classes are in:
\src\main\resources
And jsp files in:
src\main\webapp
Your .java files must be in
\src\main\java
check your dependencies for the class whether it is exist or not
modelos.Mascota

Unable to import Spring Security #Secured Annotation into Grails 3

I am currently following the Spring Security 3.0.0.M1 plugin tutorial for Grails here and I appear to be stuck on Step 8. Using the statement import grails.plugin.springsecurity.annotation.Secured does not work because Grails cannot resolve the package name. I know that Spring Security for Grails 3 is in its infancy, but has anyone been able to get past this step yet? For reference, here is my SecureController class (with a another import that also does not work):
package ldaptest.controllers
import grails.plugin.springsecurity.annotation.Secured;
import org.springframework.security.access.annotation.Secured;
#Secured('ROLE_ADMIN')
class SecureController {
def index() {
render 'Secure access only'
}
}
I may found a solution:
Create a "lib" folder e.g. inside your "grails-app" directory.
Download the SpringSecurityCore JAR from here and move it into the lib directory
Add gradle dependency:
compile files('lib/spring-security-core-3.0.0.M1.jar')
Hope this helps.
Greetings
I had:3,1,1 the save problem with my application. I solved it by adding as a library to my project. However I had to change import package to make it work.
import org.springframework.security.access.annotation.Secured
I am using IntelliJ IDEA, I just has to search the maven repo for the spring-security-core:3.1.1.
In IntelliJ you do : File > Project Structure > Libraries > Add > From Maven Repository. Then do the search according to the version of "spring-security-core" you want to use.

Resources