This paper presents an automated trajectory planning method for motorway on-ramp merging and gap development based on connected and automated vehicles. The method is composed of two relevant optimal control problems - one for the mainline facilitating vehicle and one for the on-ramp merging vehicle. Previous studies have shown that the speed of facilitating maneuver of a mainline vehicle to develop a suitable gap for accommodating an on-ramp merging vehicle could drop to an undesired low level under some conditions. To address this issue, this paper formulates the trajectory planning task of the facilitating vehicle as a state-constrained optimal control problem. The optimal control problem is constrained in the control variable as well. Moreover, the optimal control problem of the facilitating vehicle features flexible terminal time (merge location) which is automatically determined in the optimization process. The Pontryagin Maximum Principle is applied to solve the optimal control problems. The solutions are then implemented in a recursive fashion so as to accommodate constantly changing external environment. A numerical example is conducted to demonstrate the effectiveness of the proposed methodology.