We address the Merton problem of maximizing the expected utility of terminal wealth using techniques from variational analysis. Under a general continuous semimartingale market model with stochastic parameters, we obtain a characterization of the optimal portfolio for general utility functions in terms of a forward-backward stochastic differential equation (FBSDE) and derive solutions for a number of well-known utility functions. Our results complement a previous studies conducted on optimal strategies in markets driven by Brownian noise with random drift and volatility parameters.