still not find package, after 'mvn install'

2019-07-24 13:03发布

I'm trying to use Google protocol buffer for Java(I'm a newbie about Java, just trying).

First of all, I'm using OSX and I've installed protocol buffer with brew install protobuf command.

protoc command works okay.

I've generated MyProtocol.java by protoc.

I've installed protocol buffer for Java as its instruction(README.txt).

mvn install command created .m2 directory and I can find protobuf-java-2.4.1.jar file somewhere in the directory.

I wrote simple test Java code just importing MyProtocol.java and it complains could not find package com.google.protobuf.

So, I've just make jar file mvn package and add its directory as CLASSPATH and it compiled well.

javac -classpath <protobuf-dir>/jara/target/classes Test.java ./com/example/tutorial/AddressBookProtos.java

If I use maven's repository directory (~/.m2/repository), it complains again.

Doesn't maven make CLASSPATH for me if I install the package? If not, how can I use installed package by maven to compile my code?

2条回答
我想做一个坏孩纸
2楼-- · 2019-07-24 13:29

Maven doesn't change your setup, so it's up to you to set your CLASSPATH according to your need.

However there are at least two ways in which Maven can halp you:

  • You can use the Maven Assembly plugin to collect all your dependencies in a directory or an archive, so that you only need to add a single directory to your CLASSPATH in order to run your program;
  • You can use the Exec Maven plugin to add the running of your program as a goal in your POM and then run it with Maven.
查看更多
Melony?
3楼-- · 2019-07-24 13:33

The Maven Dependency Plugin has a goal called build-classpath which does what you need. You can get usage hints on the Usage page

查看更多
登录 后发表回答