mac maven cannot find symbol wrong servlet-api.jar - macos

I compile spring webflow samples project using maven in mac os and got errors. (The project compiled successfully in windows)
So I create a simple maven project to reproduce the error.
pom.xml
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<dependencies>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
src/main/java/Test.java
import javax.servlet.ServletContext;
public class Test {
public void init(ServletContext context) {
context.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
}
}
mvn compile
The error is:
I thought is was something that maven used a wrong servlet-api.jar but not servlet as pom dependency specified which is correct. (I use javac -cp javax.servlet-api-3.0.1.jar Test.java, result no error)
I check mvn dependency:tree, the result show correct servlet-api-3.0.1.jar.
I also chech mvn script to see if there is some option to set classpath.
But I just can't figure out how maven use which jars as it's classpath.

It's very weird.
After I've imported this test maven project into eclipse,
I run mvn compile again, no error happened.

Related

Create executable JAR from Groovy script

I have a maven project which consist of 1 groovy script (src/main/groovy/Main.groovy)
I am able to run it from IntelliJ by simply clicking run. What I want to do is to create an executable jar containing all dependencies so I could run in with
java -jar myjar.jar
Here's my pom:
<?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>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
</project>
I've searched on google and on so but couldn't find any working solution.
Using Maven you can set up build plugins, and one of these can be a Groovy compiler.
I wrote a blog article on setting up a Groovy compiler in Maven. But there's two parts: the groovy compiler and a plugin to package the .class files into the .jar. Traditionally for the former you'd use the groovy-eclipse-compiler and the maven-shade-plugin for the latter.
It's like 100 lines of XML in your pom file.

Why doesn't my Jsoup Maven project run?

Hi I am new to Maven and JSoup and trying to run an example in Intellij so I can get an understanding of JSoup.
I added the dependencies of JSoup to my POM.xml file. I pushed to Github when the project was working. As soon as I pulled from Github the project won't run. So I went into the terminal and getting JSoup errors that certain imports don't exist. I tried to 'run' as a Maven project but now I'm even more confused.
Please see the image below which demonstrates the file structure. The code is simple its the config / setup that I am really struggling with.
Project Structure
The POM.xml content is seen below:
<?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>WebScraper</groupId>
<artifactId>WebScraper</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<!-- jsoup HTML parser library # http://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
</dependencies>
</project>
Hopefully someone can help with this.
Thanks

IntelliJ can't find maven dependencies for GAE

I was following along with the Creating a Simple Hello World Backend API tutorial (GAE) and all was running fine via local terminal but as soon as I opened the project in IntelliJ, IntelliJ can't build the project...
The pom file has a bunch of errors about being unable to find any dependencies, however I can still build the application just fine outside of IntelliJ.
I'm not overly familiar with Maven but I believe it is configured correctly in the IDE as I can build other generic examples generated by IntelliJ. I've attempted to re-import all dependencies but no luck...
Is there something else I need to do here?
Snippet from the pom file where both dependencies are failing to find versions.
<?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>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.helloworld</groupId>
<artifactId>helloworld</artifactId>
<properties>
<app.id>your-app-id</app.id>
<app.version>1</app.version>
<appengine.version>${appengine.version}</appengine.version>
<gcloud.plugin.version>0.9.58.v20150505</gcloud.plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
</properties>
<prerequisites>
<maven>3.1.0</maven>
</prerequisites>
<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.version}</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-endpoints</artifactId>
<version>${appengine.version}</version>
</dependency>
Your appengine.version property is recursively defined, so IntelliJ can't figure out what it is. Substitute <appengine.version>${appengine.version}</appengine.version> with <appengine.version>1.9.24</appengine.version> and you should be fine.

how to automatically update maven dependencies in a multi-level project?

At my workplace, different groups works on providing different services.
My team consumes these services. Currently, whenever a new version of Service is rolled out, we manually change the pom.xml to the latest version of dependency and then make a build. I am wondering if there is an automatic way of pulling latest release into build.
Here is an example to explain:
pom.xml
<?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">
<groupId>com.company.product</groupId>
<artifactId>User-Application</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<Service1-version>1.0.2</Service-1>
<Service2-version>1.1.2</Service-2>
<Service3-version>2.0.2</Service-3>
</properties>
<dependencies>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>Service1</artifactId>
<version>${Service1-version}</version>
</dependency>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>Service2</artifactId>
<version>${Service2-version}</version>
</dependency>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>Service3</artifactId>
<version>${Service3-version}</version>
</dependency>
.....
....
</project>
When new release of each service is made, we manually change the pom.xml to get the latest dependency. How can this be managed automatically?
The versions-maven-plugin should be able to do this with the task versions:update-properties. See an example here.

Maven and dependencies

I have a simply POM 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>com.sim</groupId>
<artifactId>log4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.sim</groupId>
<artifactId>sim-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../sim-java/pom.xml</relativePath>
</parent>
<name>log4j</name>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
</dependency>
</dependencies>
I run mvn clean package on this project and a JAR is created as expected.
When I navigate into this JAR, I thought that I would see a JAR named log4j-1.2.17.jar inside it but it's not the case.
Using dependency with compile scope does not include JAR into packaging version of project ?
Thank you for clarification
The jar:jar plugin of maven just compiles your source and bundles it into a jar. Just like building a jar out of ant or bare hands, no dependency jars will be bundled in the jar. Jars cannot have dependent jars bundled inside them and even if they did, they cannot be loaded by the default class loader.
If you are looking to build a ejb-jar, then you might want to consider a EJB plugin
Check this link for various plugins that you can exploit.

Resources