Reading GTFS-realtime with java : including an existing package - java

I want to read protobuffer GTFS-realtime inputs using java, and this package
https://github.com/google/gtfs-realtime-bindings/blob/master/java/README.md
I created first a java project using Maven:
mvn archetype:generate -DgroupId=com.me.app -DartifactId=myproject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd myproject
mvn package
...went fine.
According to the documentation of the GTFS-realtime binding package, the following should be included in the pom.xml:
<dependency>
<groupId>com.google.transit</groupId>
<artifactId>gtfs-realtime-bindings</artifactId>
<version>0.0.4</version>
</dependency>
In order to avoid duplicates (which made the compilation crashed) I removed the following lines:
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
so I got this pom.xml:
<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>com.me.app</groupId>
<artifactId>myproject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myproject</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.google.transit</groupId>
<artifactId>gtfs-realtime-bindings</artifactId>
<version>0.0.4</version>
</dependency>
</dependencies>
</project>
The compilation then crashed. I am a newcomer in java and may have missed a point about how to include an existing package into a new project. Does anyone see what I am doing wrong?

Related

Cannot import json-simple with Maven

I am trying to import json-simple with maven using :
<?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>JSONtest</groupId>
<artifactId>JSONtest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
I tried to use the library, but it does not appear to me. (there should be a package called json)
import picture
Then I found out that I am getting this error : Failed to read artifact descriptor for com.googlecode.json-simple:json-simple:jar:1.1.1
I am pretty new to maven, is there something I am missing ?
mvn install:install-file -Dfile=jarlocation/json-simple.1.1.1.jar -DgroupId=com.googlecode.json-simple -DartifactId=json-simple -Dversion=1.1.1 -Dpackaging=jar
Run this command in Command Prompt(cmd), then it will be available in you local maven repository.

How to inherit dependencies from other libraries in maven?

I created a custom my-commons library with maven. The commons pom contains eg the following dependency:
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>de.mydomain</groupId>
<artifactId>my-commons</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<!-- workaround for http://jira.codehaus.org/browse/MASSEMBLY-603-->
<maven.build.timestamp>${maven.build.timestamp}</maven.build.timestamp>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
I installed the custom library into my local repository.
In a different project, I reuse the library. It resolves correctly, so it exists in the repo:
<project ...>
<groupId>de.mydomain</groupId>
<artifactId>my-core</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>my.domain</groupId>
<artifactId>my-commons</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Now in this project I'd like to use a class org.springframework.web.bind.annotation.ResponseBody.
Problem: the class is not in classpath. So maven complains [ERROR] cannot find symbol.
But why?? I'd expect the dependency being inherited!
Sidenote: I'm using Intellij IDEA if that matters. The same problem applies to all "inherited" libraries. The spring-web lib is just an example.
Please check if .m2 folder contains the jar you want to use or not. You can use mvn install command in commons project to generate the jar. Also you need to add commons project in your project pom that you are running.
You need to create the artifact for your commons project, through its pom.xml.
Like in my below example i created a separate project where i am using the AWS and some other dependency and then final jar, which i am using in other project :-
My AWS-client pom.xml looks like below :-
<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.kaamkaj</groupId>
<artifactId>aws-clients</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>aws-clients</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<aws.sdk.version>1.11.104</aws.sdk.version>
</properties>
<dependencies>
<!--AWS dependency-->
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.mercadopago</groupId>
<artifactId>sdk</artifactId>
<version>0.3.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>mercadopago</id>
<url>https://github.com/mercadopago/sdk-java/raw/master/releases</url>
</repository>
</repositories>
</project>
Also, run mvn clean install on the commons pom, in your case.
And then in all other project where i need to include the aws-client , i use the artifact-id of above project, like below :-
<dependency>
<groupId>com.kaamkaj</groupId>
<artifactId>aws-clients</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Let me know if you have any questions:
Found it: removing the <maven.build.timestamp>... <property> solved the problem. I don't know why it prevented the inheritance to work properly, especially as I didn't get any errors.

Maven dependency lib path

I have created the sample maven project and it executes well as expected, however I would like to know the location of JUnit library.
I tried to find it in the local repository and my project path but I do not find the one.
pom.xml
<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>com.maven.sample</groupId>
<artifactId>sampleproject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sampleproject</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>
Maven version: 3.3.9.
The file is certainly in your Maven cache, in the .m2/repository/junit/junit/3.8.1 folder, note the pattern: .m2/repository/<groupId>/<artifactId>/<version>
If you really want to have it automatically generated by Maven, use the Maven Dependency Plugin and its build-classpath goal.
From the command line on your project run:
mvn dependency:build-classpath
It will generate as part of the build output a list of paths for all the declared dependencies, so you will be sure where to look for.
Additionally, you can use the mdep.outputFile option to have it written to a file:
mvn dependency:build-classpath -Dmdep.outputFile=classpath.txt

Importing HBC library

I'm trying to import the following library: https://github.com/twitter/hbc
Which says I must import the folllowing in my pom.xml file:
<dependencies>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>hbc-core</artifactId> <!-- or hbc-twitter4j -->
<version>2.2.0</version> <!-- or whatever the latest version is -->
</dependency>
</dependencies>
I'm totally new to Maven. I'm using Eclipse, which already had Maven installed. I created a new Maven project, and then created a new .xml file named pom.xml with the above. However, nothing seems to have been imported; library stuff isn't working in the project. For example, the option to import "httpHosts" does not exist.
How am I meant to actually import this?
As Jorge Campos, suggests you need to do some reading on basic Maven. principles. So please read this in order to get started.
In order to help you a bit this is how your pom.xml should look 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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>hbc-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
You need to take care the groupid and artifact id sections, but I hope you will find out, if you spend some time and read about Maven :)

maven version dependency injection to sub modules

I purchased the 'Apache Maven 3 Cookbook' and I'm trying to learn on adding dependencies to my maven projects.
my main project 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">
<modelVersion>4.0.0</modelVersion>
<groupId>net.srirangan.packt.maven</groupId>
<artifactId>TestModularApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>TestModularApp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies>
<modules>
<module>ChildProject</module>
<module>MyWebApp</module>
</modules>
</project>
as you can see here the packaging in this project is set to "pom" because this is the parent project.. ( that's all i know :) )
and then I created a subproject with the following pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.srirangan.packt.maven</groupId>
<artifactId>TestModularApp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.xpogames.childproject</groupId>
<artifactId>ChildProject</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ChildProject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
as you can see here i did not add a version property to the mysql-connect-java dependency because it supposed to inherit the version from the parent project. but for some reason, it doesn't.
when I run mvn compile on that project i get an error that the dependencies.dependency.version property is missing.
any ideas what i'm doing wrong? how can I resolve the issue that i won't need to specify versions in sub-projects too ?
thanks!
update
after watching Peter Lawrey's answer and watching the example on the url he provided
I noticed that my main XML is missing the property <dependencyManagement> around the <dependencies>. once I added that property then I didn't need to provide a version number in the sub-project.
Peter answer shows another method to achieve this goal.
thanks for everything!
Kfir
You inherit versions from the dependencies in the dependencyManagement section of your parent pom(s) and selectively include these in child poms.
In your case you don't need to mention the dependency again as it already included from the parent. (As it is for JUnit)
BTW: I use JUnit 4.10 which I haven't found any backward compatibility problems with.

Categories